day 72 作业
1.serializers
from rest_framework import serializers
from django.conf import settings
from . import models
class CarSerializers(serializers.ModelSerializer):
re_brand = serializers.CharField(min_length=3, max_length=16, write_only=True)
class Meta:
model = models.CarAPI
fields = ('name', 'price', 'poster', 'color', 'hue', 'icon', 'brand', 're_brand')
extra_kwargs = {
'name': {
'min_length': 3,
'max_length': 16,
'error_messages': {
'min_length': '太短',
'max_length': '太长'
}
},
'hue': {
'read_only': True
},
'color': {
'write_only': True
},
'icon': {
'read_only': True
},
# 're_brand': {
# 'write_only': True
# }
}
def validate(self, attrs):
brand = attrs.get('brand')
re_brand = attrs.pop('re_brand')
print(brand,re_brand)
if brand != re_brand:
raise serializers.ValidationError({'re_brand': '品牌不一致'})
return attrs
models
from django.db import models
# Create your models here.
class CarAPI(models.Model):
color_choices = (
(1, '银色'),
(2, '黑色'),
(3, '红色'),
(4, '蓝色'),
(5, '绿色'),
(6, '黄色'),
)
name = models.CharField(max_length=32, verbose_name='名字')
color = models.IntegerField(choices=color_choices, default=1, verbose_name='车身颜色')
price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='售价')
poster = models.ImageField(upload_to='img', null=True, blank=True, verbose_name='海报')
brand = models.CharField(max_length=32, verbose_name='品牌')
def hue(self):
return self.get_color_display()
def icon(self):
from django.conf import settings
return '%s%s%s' % (settings.BASE_URL, settings.MEDIA_URL, self.poster)
class Meta:
verbose_name_plural = '汽车表'
def __str__(self):
return self.name
views
from django.shortcuts import render
from api import models
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.renderers import JSONOpenAPIRenderer
from rest_framework.parsers import JSONParser, FormParser, MultiPartParser
from . import serializers
# Create your views here.
class BookApi(APIView):
renderer_classes = [JSONOpenAPIRenderer, ]
parser_classes = [JSONParser, FormParser, MultiPartParser]
def get(self, request, *args, **kwargs):
if kwargs:
return Response({
'status': 0,
'msg': 'get ok',
'results': [request.query_params, request.data]
}, status=404)
return Response({
'status': 0,
'msg': 'get ok'
})
def post(self, request, *args, **kwargs):
if kwargs:
return Response({
'status': 0,
'msg': 'post ok',
'results': [request.query_params, request.data]
})
return Response({
'status': 0,
'msg': 'post ok',
'results': [request.query_params, request.data]
})
class CarApi(APIView):
def get(self, request, *args, **kwargs):
pk = kwargs.get('pk')
if pk:
car_dic = models.CarAPI.objects.filter(pk=pk).first()
if not car_dic:
return Response({
'status': 1,
'msg': 'pk error'
}, status=404)
car_ser = serializers.CarSerializers(car_dic, many=False)
return Response({
'status': 0,
'msg': 'ok',
'results': car_ser.data
})
else:
car_list = models.CarAPI.objects.all()
car_ser = serializers.CarSerializers(car_list, many=True)
return Response({
'status': 0,
'msg': 'ok',
'results': car_ser.data
})
def post(self, request, *args, **kwargs):
car_ser = serializers.CarSerializers(data=request.data)
if car_ser.is_valid():
# print(car_ser.data)
car_obj = car_ser.save()
return Response({
'status': 0,
'msg': 'ok',
'results': serializers.CarSerializers(car_obj).data
})
else:
return Response({
'status': 1,
'msg': car_ser.errors
})