我使用自己的模型浏览了drf网站的快速入门教程,其中包含许多m2m和foreignkey关系。我的一些m2m关系通过使用'self'来指代相同的模型。现在,当我查看我的api以获取那些m2m和外键关系时,我只看到了id号而不是其他信息。如何更改此信息以包含有用信息?另外,如何限制信息的显示深度?我觉得如果我能获得除id之外的其他信息,m2m关系可以永远持续下去。
无论如何,这里是我现在可以看到的片段:我希望外观和relatives_many显示模型的json,或者至少只显示除id之外的名称字段。谢谢你的帮助!
{
"id": 97,
"name": "Ancient Sleeping Magi of Life Giving's mother",
"full_name": "Ancient Sleeping Magi of Life Giving's mother",
"sex": "Female",
"link": "http://adventuretime.wikia.com/wiki/Ancient_Sleeping_Magi_of_Life_Giving%27s_mother",
"image": "http://img2.wikia.nocookie.net/__cb20130408224627/adventuretimewithfinnandjake/images/0/0b/Magi%27s_parents1.png",
"species": [],
"appearance": [
245
],
"relatives_many": [
96,
92,
94,
705
]
},
我的部分模特。
class Episode(models.Model):
title = models.CharField(max_length=80)
description = models.TextField(blank=True)
season_number = models.PositiveSmallIntegerField(blank=True, null=True)
episode_number = models.PositiveSmallIntegerField(blank=True, null=True)
def __unicode__(self):
return u"{}".format(self.title)
class Character(models.Model):
name = models.CharField(max_length=80)
full_name = models.CharField(max_length=120)
sex = models.CharField(max_length=100, blank=True)
species = models.ManyToManyField(Species, related_name="characters", blank=True)
link = models.URLField(blank=True)
appearance = models.ManyToManyField(Episode, related_name="characters", blank=True)
image = models.URLField(blank=True)
relatives_many = models.ManyToManyField('self', related_name="characters", blank=True)
def __unicode__(self):
return u"{}".format(self.name)
串行器。我已经尝试设置fields =(“something”,....)但这并没有改变id的显示方式。
class CharacterSerializer(serializers.ModelSerializer):
class Meta:
model = Character
class SpeciesSerializer(serializers.ModelSerializer):
class Meta:
model = Species
视图。
class CharacterViewSet(viewsets.ModelViewSet):
queryset = Character.objects.all()
serializer_class = CharacterSerializer
class SpeciesViewSet(viewsets.ModelViewSet):
queryset = Species.objects.all()
serializer_class = SpeciesSerializer
编辑按照sax的建议,我明白了:
{
"relatives": [
{
"id": 38,
"name": "Ice King",
"full_name": "Ice King",
"sex": "Male",
"link": "http://adventuretime.wikia.com/wiki/Ice_King",
"image": "http://img3.wikia.nocookie.net/__cb20120921153231/adventuretimewithfinnandjake/images/6/64/Original_Ice_King.png",
"species": [
10,
12,
14
],
"appearance": [
42,
23
]
}
],
"id": 42,
"name": "Princess Monster Wife",
"full_name": "",
"sex": "",
"link": "",
"image": "",
"species": [],
"appearance": []
},
答案 0 :(得分:3)
试试这个,它没有经过测试,但类似的代码对我有用:
class ChildCharacterSerializer(serializers.ModelSerializer):
class Meta:
model = Character
exclude = ('relatives_many',)
class CharacterSerializer(serializers.ModelSerializer):
relatives = serializers.SerializerMethodField('get_relatives')
class Meta:
model = Character
def get_relatives(self, character):
qs = character.characters.all()
serializer = ChildCharacterSerializer(instance=qs,
many=True, context=self.context)
return serializer.data
或者如果您不需要特殊过滤器:
class CharacterSerializer(serializers.ModelSerializer):
relatives_many = ChildCharacterSerializer()