假设我有这个模型和序列化器:
class Animal(models.Model):
id = models.AutoField(primary_key=True)
data_url = models.TextField(unique=True)
class AnimalSerializer(serializers.ModelSerializer):
class Meta:
model = Animal
fields = '__all__'
如果我跑步:
animal = Animal.objects.filter(id=1).first()
data = serializers.serialize("json", animal)
我得到一个看起来像这样的json字符串:
[{"model": "zoo.animal", "pk": 1, "fields": {"id": "1", "sound": "woof"}]
现在,如果我跑步:
animal = Animal.objects.filter(id=1).first()
animal_serialized = AnimalSerializer(animal)
animal = JSONRenderer().render(animal.data)
我得到一个看起来像这样的json字符串:
b'[{"id":1,"sound":"woof"}]'
但是,此json字符串缺少“ model”,“ pk”和“ fields”字典,仅是字段。如何使我的AnimalSerializer也返回此数据,就像我运行serializers.serialize(“ json”,animal)一样?