使用 Django shell 进行序列化调试 1 2 3 4 5 6 7 (web12) leazhi@ubuntuhome:~$ cd web12/ (web12) leazhi@ubuntuhome:~/web12$ python manage.py shell Python 3.10 .12 (main, Jun 11 2023 , 05:26 :28 ) [GCC 11.4 .0 ] on linux Type "help" , "copyright" , "credits" or "license" for more information.(InteractiveConsole) >>> from users.serializer import BookInfoSerializers,PeopleInfoSerializers>>> from users.models import BookInfo
单个数据序列化 1 2 3 4 5 6 7 8 9 10 >>> book = BookInfo.objects.get(id =2 )>>> ser = BookInfoSerializers(instance= book)>>> ser.data
多个数据 1 2 3 4 5 6 7 8 9 10 11 >>> book = BookInfo.objects.all ()>>> ser = BookInfoSerializers(instance=book, many=True )>>> ser.data
关联查询对象的序列化 1.编辑子应用下的 serializer.py 文件,先导入模型类 PeopleInfo, 然后编写关联查询的序列化代码,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ... from users.models import PeopleInfo... class PeopleInfoSerializer (serializers.ModelSerializer): GENDER_CHOICE = ( (0 , 'male' ), (1 , 'female' ), ) name = serializers.CharField(max_length=20 , label='名称' ) gender = serializers.IntegerField(label='性别' ) description = serializers.CharField(max_length=200 , label='描述信息' ) book = serializers.StringRelatedField(label='图书' ) is_delete = serializers.BooleanField(label='逻辑删除' , default=0 ) class Meta : model = PeopleInfo fields = '__all__'
2.进入 django shell 调试窗口,执行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 (web12) leazhi@ubuntuhome:~/web12$ python manage.py shell Python 3.10 .12 (main, Jun 11 2023 , 05:26 :28 ) [GCC 11.4 .0 ] on linux Type "help" , "copyright" , "credits" or "license" for more information.(InteractiveConsole) >>> from users.serializer import PeopleInfoSerializer>>> from users.models import PeopleInfo>>> >>> person = PeopleInfo.objects.get(id =18 ) >>> person<PeopleInfo: 郭靖> >>> >>> ser = PeopleInfoSerializer(person) >>> ser.data{'id' : 18 , 'name' : '郭靖' , 'gender' : 1 , 'description' : '降龙十八掌' , 'book' : '射雕英雄传' , 'is_delete' : False } >>> >>> person = PeopleInfo.objects.get(id =1 ) Traceback (most recent call last): File "<console>" , line 1 , in <module> File "/home/leazhi/.env/web12/lib/python3.10/site-packages/django/db/models/manager.py" , line 85 , in manager_method return getattr (self.get_queryset(), name)(*args, **kwargs) File "/home/leazhi/.env/web12/lib/python3.10/site-packages/django/db/models/query.py" , line 429 , in get raise self.model.DoesNotExist( users.models.PeopleInfo.DoesNotExist: PeopleInfo matching query does not exist.
1.编辑子应用下的 serializer.py 文件,先导入模型类 PeopleInfo, 然后编写关联查询的序列化代码,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ... from users.models import PeopleInfo... class PeopleInfoSerializer (serializers.ModelSerializer): GENDER_CHOICE = ( (0 , 'male' ), (1 , 'female' ), ) name = serializers.CharField(max_length=20 , label='名称' ) gender = serializers.IntegerField(label='性别' ) description = serializers.CharField(max_length=200 , label='描述信息' ) book = PeopleInfoSerializer() is_delete = serializers.BooleanField(label='逻辑删除' , default=0 ) class Meta : model = PeopleInfo fields = '__all__'
2.进入 django shell 调试窗口,执行:
1 2 3 4 5 6 7 8 9 10 11 12 13 (web12) leazhi@ubuntuhome:~/web12$ python manage.py shell Python 3.10 .12 (main, Jun 11 2023 , 05:26 :28 ) [GCC 11.4 .0 ] on linux Type "help" , "copyright" , "credits" or "license" for more information.(InteractiveConsole) >>> from users.serializer import PeopleInfoSerializer>>> from users.models import PeopleInfo>>> person = PeopleInfo.objects.get(id =18 )>>> person<PeopleInfo: 郭靖> >>> ser = PeopleInfoSerializer(person)>>> ser.data{'id' : 18 , 'name' : '郭靖' , 'gender' : 1 , 'description' : '降龙十八掌' , 'book' : OrderedDict([('id' , 1 ), ('name' , '射雕英雄传' ), ('pub_date' , '1980-05-01' )]), 'is_delete' : False } >>>