标签:register date include def site package lower 访问 view
from rest_framework.viewsets import ModelViewSet from .models import BookInfo from .serializers import BookInfoSerializers class BookInfoViewSet(ModelViewSet): queryset = BookInfo.objects.all() # 取序列化模型 serializer_class = BookInfoSerializers # 指定序列化的类
from django.conf.urls import url, include from . import views from rest_framework.routers import DefaultRouter router = DefaultRouter() router.register(r"books", views.BookInfoViewSet) urlpatterns = [ url(r‘^index/$‘, views.index), ] urlpatterns += router.urls
http://127.0.0.1:8000/app001/books/
class BookInfo(models.Model): btitle = models.CharField(max_length=20, verbose_name=‘名称‘) bpub_date = models.DateField(verbose_name=‘发布日期‘) bread = models.IntegerField(default=0, verbose_name=‘阅读量‘) bcomment = models.IntegerField(default=0, verbose_name=‘评论量‘) is_delete = models.BooleanField(default=False, verbose_name=‘逻辑删除‘) class Meta: db_table = ‘tb_books‘ # 指明数据库表名 verbose_name = ‘图书‘ # 在admin站点中显示的名称 verbose_name_plural = verbose_name # 显示的复数名称 def __str__(self): return self.btitle
from rest_framework import serializers class BookInfoSerializers(serializers.Serializer): """ 图书列表序列化器""" id = serializers.IntegerField(label="ID", read_only=True) btitle = serializers.CharField(label="名称", max_length=20) bpub_date = serializers.DateField(label=‘发布日期‘, required=False) bread = serializers.IntegerField(label=‘阅读量‘, required=False) bcomment = serializers.IntegerField(label=‘评论量‘, required=False) is_delete = serializers.BooleanField(label=‘逻辑删除‘, required=False)
# 查询图书的的对象 from app001.models import BookInfo book = BookInfo.objects.all()[0] # 构造序列化器 from app001.serializers import BookInfoSerializers serializer = BookInfoSerializers(book) # 获取序列化之后的属性 print(serializer.data)
# 打印效果: """ {‘id‘: 1, ‘bpub_date‘: ‘1980-05-01‘, ‘btitle‘: ‘射雕英雄传‘, ‘is_delete‘: False, ‘bcomment‘: 34, ‘bread‘: 12} """
class HeroInfo(models.Model): GENDER_CHOICES = ( (0, ‘male‘), (1, ‘female‘) ) hname = models.CharField(max_length=20, verbose_name=‘名称‘) hgender = models.SmallIntegerField(choices=GENDER_CHOICES, default=0, verbose_name=‘性别‘) hcomment = models.CharField(max_length=200, null=True, verbose_name=‘描述信息‘) hbook = models.ForeignKey(BookInfo, on_delete=models.CASCADE, verbose_name=‘图书‘) # 外键 is_delete = models.BooleanField(default=False, verbose_name=‘逻辑删除‘) class Meta: db_table = ‘tb_heros‘ verbose_name = ‘英雄‘ verbose_name_plural = verbose_name def __str__(self): return self.hname
from rest_framework import serializers class HeroInfoSerializers(serializers.Serializer): """英雄数据序列化器""" GENDER_CHOICES = ( (0, ‘male‘), (1, ‘female‘) ) # hname = models.CharField(max_length=20, verbose_name=‘名称‘) id = serializers.IntegerField(label=‘ID‘, read_only=True) # hgender = models.SmallIntegerField(choices=GENDER_CHOICES, default=0, verbose_name=‘性别‘) hgender = serializers.ChoiceField(choices=GENDER_CHOICES, label=‘性别‘, required=False) # hcomment = models.CharField(max_length=200, null=True, verbose_name=‘描述信息‘) hcomment = serializers.CharField(label=‘描述信息‘, max_length=200, required=False, allow_null=True) # hbook = models.ForeignKey(BookInfo, on_delete=models.CASCADE, verbose_name=‘图书‘) # 外键 hbook = serializers.PrimaryKeyRelatedField(label=‘图书‘, read_only=True) # is_delete = models.BooleanField(default=False, verbose_name=‘逻辑删除‘)
from app001.models import HeroInfo hero = HeroInfo.objects.all()[0] from app001.serializers import HeroInfoSerializers hs = HeroInfoSerializers(hero) print(hs.data)
from app001.models import BookInfo book = BookInfo.objects.all()[0] # 构造序列化器 from app001.serializers import BookInfoSerializers bs = BookInfoSerializers(book) # 获取序列化之后的属性 print(bs.data)
from app001.serializers import BookInfoSerializers data = {‘bpub_date‘: 123} serializer = BookInfoSerializers(data=data) serializer.is_valid() serializer.errors serializer.validated_data
from app001.serializers import BookInfoSerializers data = {‘bpub_date‘: "1993-12-27", ‘btitle‘: ‘python‘} serializer = BookInfoSerializers(data=data) serializer.is_valid() serializer.errors serializer.validated_data
from rest_framework import serializers class BookInfoSerializers(serializers.Serializer): """ 图书列表序列化器""" id = serializers.IntegerField(label="ID", read_only=True) btitle = serializers.CharField(label="名称", max_length=20) bpub_date = serializers.DateField(label=‘发布日期‘, required=False) bread = serializers.IntegerField(label=‘阅读量‘, required=False) bcomment = serializers.IntegerField(label=‘评论量‘, required=False) is_delete = serializers.BooleanField(label=‘逻辑删除‘, required=False) heroinfo_set = serializers.PrimaryKeyRelatedField(read_only=True, many=True) # 新增 def validate_btitle(self, value): if ‘django‘ not in value.lower(): raise serializers.ValidationError("图书不是关于Django的") return value
from rest_framework import serializers def about_django(value): if ‘django‘ not in value.lower(): raise serializers.ValidationError("about_django中抛出图书不是关于Django的") class BookInfoSerializers(serializers.Serializer): """ 图书列表序列化器""" id = serializers.IntegerField(label="ID", read_only=True) btitle = serializers.CharField(label="名称", max_length=20,validators=[about_django]) bpub_date = serializers.DateField(label=‘发布日期‘, required=False) bread = serializers.IntegerField(label=‘阅读量‘, required=False) bcomment = serializers.IntegerField(label=‘评论量‘, required=False) is_delete = serializers.BooleanField(label=‘逻辑删除‘, required=False) heroinfo_set = serializers.PrimaryKeyRelatedField(read_only=True, many=True) # 新增 def validate(self, attrs): bread = attrs[‘bread‘] bcomment = attrs[‘bcomment‘] if bread < bcomment: raise serializers.ValidationError(‘阅读量小于评论量‘) return attrs
>>> from app001.serializers import BookInfoSerializers >>> data = {‘bcomment‘: 34, ‘bread‘: 12,‘bpub_date‘: "1993-12-27", ‘btitle‘: ‘python_django‘} >>> serializer = BookInfoSerializers(data=data) >>> serializer.is_valid() False >>> serializer.errors {‘non_field_errors‘: [ErrorDetail(string=‘阅读量小于评论量‘, code=‘invalid‘)]} >>> serializer.validated_data {}
>>> from app001.serializers import BookInfoSerializers >>> data = {‘bcomment‘: 14, ‘bread‘: 12,‘bpub_date‘: "1993-12-27", ‘btitle‘: ‘ptython‘} >>> serializer = BookInfoSerializers(data=data) >>> serializer.is_valid() False >>> serializer.errors {‘btitle‘: [ErrorDetail(string=‘about_django中抛出图书不是关于Django的‘, code=‘invalid‘)]} >>> serializer.validated_data {} >>>
from rest_framework import serializers class BookInfoSerializers(serializers.Serializer): """ 图书列表序列化器""" id = serializers.IntegerField(label="ID", read_only=True) btitle = serializers.CharField(label="名称", max_length=20,validators=[about_django]) bpub_date = serializers.DateField(label=‘发布日期‘, required=False) bread = serializers.IntegerField(label=‘阅读量‘, required=False) bcomment = serializers.IntegerField(label=‘评论量‘, required=False) is_delete = serializers.BooleanField(label=‘逻辑删除‘, required=False) heroinfo_set = serializers.PrimaryKeyRelatedField(read_only=True, many=True) # 新增 def validate_btitle(self, value): if ‘django‘ not in value.lower(): raise serializers.ValidationError("图书不是关于Django的") return value
>>> from app001.serializers import BookInfoSerializers >>> data = {‘bcomment‘: 10, ‘bread‘: 12,‘bpub_date‘: "1993-12-27", ‘btitle‘: ‘python_django‘} >>> serializer = BookInfoSerializers(data=data) >>> serializer.is_valid() True >>> serializer.errors {} >>> serializer.validated_data OrderedDict([(‘btitle‘, ‘python_django‘), (‘bpub_date‘, datetime.date(1993, 12, 27)), (‘bread‘, 12), (‘bcomment‘, 10)]) >>>
>>> from app001.serializers import BookInfoSerializers >>> data = {‘bcomment‘: 10, ‘bread‘: 12,‘bpub_date‘: "1993-12-27", ‘btitle‘: ‘ptython‘} >>> serializer = BookInfoSerializers(data=data) >>> serializer.is_valid() False >>> serializer.errors {‘btitle‘: [ErrorDetail(string=‘图书不是关于Django的‘, code=‘invalid‘)]} >>> serializer.validated_data {}
from rest_framework import serializers def about_django(value): if ‘django‘ not in value.lower(): raise serializers.ValidationError("图书不是关于Django的") class BookInfoSerializers(serializers.Serializer): """ 图书列表序列化器""" id = serializers.IntegerField(label="ID", read_only=True) btitle = serializers.CharField(label="名称", max_length=20,validators=[about_django]) bpub_date = serializers.DateField(label=‘发布日期‘, required=False) bread = serializers.IntegerField(label=‘阅读量‘, required=False) bcomment = serializers.IntegerField(label=‘评论量‘, required=False) is_delete = serializers.BooleanField(label=‘逻辑删除‘, required=False) heroinfo_set = serializers.PrimaryKeyRelatedField(read_only=True, many=True) # 新增 def validate_btitle(self, value): if ‘django‘ not in value.lower(): raise serializers.ValidationError("图书不是关于Django的") return value def validate(self, attrs): bread = attrs[‘bread‘] bcomment = attrs[‘bcomment‘] if bread < bcomment: raise serializers.ValidationError(‘阅读量小于评论量‘) return attrs
>>> from app001.serializers import BookInfoSerializers >>> data = {‘bcomment‘: 10, ‘bread‘: 12,‘bpub_date‘: "1993-12-27", ‘btitle‘: ‘ptython_django‘} >>> serializer = BookInfoSerializers(data=data) >>> serializer.is_valid() True >>> serializer.errors {} >>> serializer.validated_data OrderedDict([(‘btitle‘, ‘ptython_django‘), (‘bpub_date‘, datetime.date(1993, 12, 27)), (‘bread‘, 12), (‘bcomment‘, 10)])
>>> from app001.serializers import BookInfoSerializers >>> data = {‘bcomment‘: 10, ‘bread‘: 12,‘bpub_date‘: "1993-12-27", ‘btitle‘: ‘ptython‘} >>> serializer = BookInfoSerializers(data=data) >>> serializer.is_valid() False >>> serializer.errors {‘btitle‘: [ErrorDetail(string=‘about_django中抛出图书不是关于Django的‘, code=‘invalid‘)]} >>> serializer.validated_data {}
from rest_framework.validators import UniqueValidator slug = SlugField( max_length=100, validators=[UniqueValidator(queryset=BlogPost.objects.all())] )
from rest_framework.validators import UniqueTogetherValidator class ExampleSerializer(serializers.Serializer): # ... class Meta: validators = [ UniqueTogetherValidator( queryset=ToDoItem.objects.all(), fields=(‘list‘, ‘position‘) ) ]
>>> from app001.serializers import BookInfoSerializers >>> data = {‘btitle‘: ‘封神演义‘} >>> serializer = BookInfoSerializers(data=data) >>> serializer.is_valid() False >>> serializer.save() Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/python/.virtualenvs/djf/lib/python3.5/site-packages/rest_framework/serializers.py", line 184, in save ‘You cannot call `.save()` on a serializer with invalid data.‘ AssertionError: You cannot call `.save()` on a serializer with invalid data.
>>> from app001.serializers import BookInfoSerializers >>> data = {‘bcomment‘: 10, ‘bread‘: 12,‘bpub_date‘: "1993-12-27", ‘btitle‘: ‘ptython_django‘} >>> serializer = BookInfoSerializers(data=data) >>> serializer.is_valid() True >>> serializer.save() <BookInfo: ptython_django>
from rest_framework import serializers def about_django(value): if ‘django‘ not in value.lower(): raise serializers.ValidationError("about_django中抛出图书不是关于Django的") class BookInfoSerializers(serializers.Serializer): """ 图书列表序列化器""" id = serializers.IntegerField(label="ID", read_only=True) btitle = serializers.CharField(label="名称", max_length=20, validators=[about_django]) bpub_date = serializers.DateField(label=‘发布日期‘, required=False) bread = serializers.IntegerField(label=‘阅读量‘, required=False) bcomment = serializers.IntegerField(label=‘评论量‘, required=False) is_delete = serializers.BooleanField(label=‘逻辑删除‘, required=False) heroinfo_set = serializers.PrimaryKeyRelatedField(read_only=True, many=True) # 新增 def validate_btitle(self, value): if ‘django‘ not in value.lower(): raise serializers.ValidationError("图书不是关于Django的") return value def create(self, validated_data): """新建""" return BookInfo.objects.create(**validated_data) def update(self, instance, validated_data): """更新,instance为要更新的对象实例""" instance.btitle = validated_data.get(‘btitle‘, instance.btitle) instance.bpub_date = validated_data.get(‘bpub_date‘, instance.bpub_date) instance.bread = validated_data.get(‘bread‘, instance.bread) instance.bcomment = validated_data.get(‘bcomment‘, instance.bcomment) instance.save() return instance
>>> from app001.serializers import BookInfoSerializers >>> # from app001.models import BookInfo >>> # book = BookInfo.objects.filter(btitle="ptython_django")[0] >>> book = BookInfo.objects.get(id=10) >>> data = {‘bcomment‘: 10, ‘bread‘: 12,‘bpub_date‘: "1994-12-27", ‘btitle‘: ‘ptython_django‘} >>> # data = {‘bpub_date‘: "1994-12-27",‘bcomment‘: 10, ‘bread‘: 12,} >>> serializer = BookInfoSerializers(book, data=data) >>> serializer.is_valid() True >>> serializer.save() <BookInfo: ptython_django> >>> book.btitle ‘ptython_django‘ >>>
>>> from app001.serializers import BookInfoSerializers >>> from app001.models import BookInfo >>> book = BookInfo.objects.filter(btitle="ptython_django")[0] >>> data = {‘bcomment‘: 10, ‘bread‘: 12,‘bpub_date‘: "1995-12-27", ‘btitle‘: ‘ptython_django‘} >>> serializer = BookInfoSerializers(book, data=data) >>> serializer.is_valid() True >>> serializer.save() <BookInfo: ptython_django> >>> book.btitle ‘ptython_django‘ >>> book.bpub_date datetime.date(1995, 12, 27)
>>> from app001.serializers import BookInfoSerializers >>> from app001.models import BookInfo >>> book = BookInfo.objects.filter(btitle="ptython_django")[0] >>> data = {‘bpub_date‘: "1996-12-27", ‘btitle‘: ‘ptython_django‘} >>> serializer = BookInfoSerializers(book, data=data) >>> serializer.is_valid() True >>> serializer.save() <BookInfo: ptython_django> >>> book.btitle ‘ptython_django‘ >>> book.bpub_date datetime.date(1996, 12, 27)
标签:register date include def site package lower 访问 view
原文地址:https://www.cnblogs.com/cerofang/p/9323140.html