车辆管理系统
题目
1 基本信息:车型(CarModel),车厂(CarFactory),经销商(Distributor)三个表,
一个车厂可以生产多种车型,一个经销商可以出售多种车型,一个车型可以有多个经销商出售
车型:车型名,车型出厂价,车厂id
车厂:车厂名,车厂地址,联系电话
经销商:经销商名,地址,联系电话
2 有用户表,基于django内置user表,扩展mobile字段
3 编写登陆接口,jwt方式返回token,
格式为{status:100,msg:登陆成功,token:safasdfa}
3 有管理员登陆后可以新增,删除车型,车厂,经销商
2 普通用户登陆可以查看车型(群查分页,单查)
查车型:返回车型信息,车厂名字,经销商名字和电话
加分项:
用户注册接口
管理员有用户锁定,删除功能
# 模型表
from django.db import models
from rest_framework import serializers
from django.contrib.auth.models import AbstractUser
# Create your models here.
class CarModel(models.Model):
""" 车型表"""
name = models.CharField(max_length=32, verbose_name='车型名')
price = models.IntegerField(verbose_name='车型出产价')
# 外键 一对多
carfactory = models.ForeignKey(to='CarFactory', on_delete=models.CASCADE, null=True)
# 多对多
distributors = models.ManyToManyField(to='Distributor')
# 定制返回格式
carfactory_detail = serializers.SerializerMethodField()
def carfactory_detail(self):
return {'name': self.carfactory.name, 'address': self.carfactory.address, 'phone': self.carfactory.phone}
distributor_list = serializers.SerializerMethodField()
def distributor_list(self):
l1 = []
for distributor in self.distributors.all():
l1.append({'name': distributor.name, 'address': distributor.address, 'phone': distributor.phone})
return l1
class CarFactory(models.Model):
""" 车产表"""
name = models.CharField(max_length=32, verbose_name='车厂名')
address = models.CharField(max_length=32, verbose_name='车厂地址')
phone = models.CharField(max_length=32, verbose_name='车厂联系电话')
class Distributor(models.Model):
""" 经销商表"""
name = models.CharField(max_length=32, verbose_name='经销商名')
address = models.CharField(max_length=32, verbose_name='经销商地址')
phone = models.CharField(max_length=32, verbose_name='经销商联系电话')
# class UserInfo(AbstractUser):
# mobile = models.BigIntegerField(verbose_name='手机号')
# 扩展的表要去setting注册# AUTH_USER_MODEL='app01.UserInfo'
# url
from django.contrib import admin
from app01 import views
from django.urls import path, include
from rest_framework_jwt.views import obtain_jwt_token
from rest_framework.routers import SimpleRouter
router = SimpleRouter()
router.register('car',views.CarView,'car')
router.register('car_factory',views.CarFactoryView,'car_factory')
router.register('car_distibutor',views.DistibutorView,'car_distibutor')
urlpatterns = [
path('admin/', admin.site.urls),
# 登录接口
path('login/',obtain_jwt_token),
path('api/v1/',include(router.urls))
]
# view
from rest_framework.viewsets import ViewSet,ModelViewSet,ViewSetMixin,ReadOnlyModelViewSet
from .models import CarModel,CarFactory,Distributor
from .serializer import CarSerializer,CarFactorySerializer,CarDistributorSerializer
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.permissions import IsAuthenticated
from .permission import CommonPerssion
from .page import CommonPageNumberPagination
# 车
class CarView(ModelViewSet):
queryset = CarModel.objects.all()
serializer_class = CarSerializer
authentication_classes = [JSONWebTokenAuthentication]
permission_classes = [IsAuthenticated,CommonPerssion]
pagination_class=CommonPageNumberPagination
# 车厂
class CarFactoryView(ModelViewSet):
queryset = CarFactory.objects.all()
serializer_class = CarFactorySerializer
authentication_classes = [JSONWebTokenAuthentication]
permission_classes = [IsAuthenticated,CommonPerssion]
pagination_class=CommonPageNumberPagination
# 经销商
class DistibutorView(ModelViewSet):
queryset = Distributor.objects.all()
serializer_class = CarDistributorSerializer
authentication_classes = [JSONWebTokenAuthentication]
permission_classes = [IsAuthenticated,CommonPerssion]
pagination_class=CommonPageNumberPagination
# settings
# 扩充模型表注册
# AUTH_USER_MODEL='app01.UserInfo'
JWT_AUTH = {
'JWT_RESPONSE_PAYLOAD_HANDLER': 'app01.utils.jwt_response_payload_handler'
}
# 设置过期期间
import datetime
JWT_RESPONSE_PAYLOAD_HANDLER = {
'JWT_EXPIRATION_DELTA': datetime.timedelta(days=7)
}
# 权限类
from rest_framework.permissions import BasePermission
class CommonPerssion(BasePermission):
def has_permission(self, request, view):
if request.user.is_superuser:
return True
else:
if request.method=='GET':
return True
else:
return False
# 分页
# 导入 PageNumberPagination
from rest_framework.pagination import PageNumberPagination
class CommonPageNumberPagination(PageNumberPagination):
page_size = 2 # 每页两条
page_query_param = 'page'
page_size_query_param='size'
max_page_size = 3
标签:name,管理系统,models,车厂,车辆,import,class,verbose
From: https://www.cnblogs.com/zhanglanhua/p/17133100.html