在 Django Rest Framework 的响应中包含中介(通过模型) [英] Include intermediary (through model) in responses in Django Rest Framework
问题描述
我有一个关于处理 m2m/through 模型及其在 django rest 框架中的演示的问题.举个经典的例子:
I have a question about dealing with m2m / through models and their presentation in django rest framework. Let's take a classic example:
models.py:
from django.db import models
class Member(models.Model):
name = models.CharField(max_length = 20)
groups = models.ManyToManyField('Group', through = 'Membership')
class Group(models.Model):
name = models.CharField(max_length = 20)
class Membership(models.Model):
member = models.ForeignKey('Member')
group = models.ForeignKey('Group')
join_date = models.DateTimeField()
serializers.py:
serializers.py:
imports...
class MemberSerializer(ModelSerializer):
class Meta:
model = Member
class GroupSerializer(ModelSerializer):
class Meta:
model = Group
views.py:
imports...
class MemberViewSet(ModelViewSet):
queryset = Member.objects.all()
serializer_class = MemberSerializer
class GroupViewSet(ModelViewSet):
queryset = Group.objects.all()
serializer_class = GroupSerializer
当 GETing 一个 Member 实例时,我成功地收到了所有成员的字段及其组 - 但是我只获得了组的详细信息,没有来自 Membership 模型的额外详细信息.
When GETing an instance of Member, I successfully receive all of the member's fields and also its groups - however I only get the groups' details, without extra details that comes from the Membership model.
换句话说,我期望收到:
{
'id' : 2,
'name' : 'some member',
'groups' : [
{
'id' : 55,
'name' : 'group 1'
'join_date' : 34151564
},
{
'id' : 56,
'name' : 'group 2'
'join_date' : 11200299
}
]
}
注意加入日期.
我尝试了很多解决方案,当然包括Django Rest-Framework 官方页面关于它 似乎没有人给出一个正确的简单答案 - 我需要做什么来包含这些额外的字段?我发现使用 django-tastypie 更直接,但还有一些其他问题,我更喜欢 rest-framework.
I have tried oh so many solutions, including of course Django Rest-Framework official page about it and no one seems to give a proper plain answer about it - what do I need to do to include these extra fields? I found it more straight-forward with django-tastypie but had some other problems and prefer rest-framework.
推荐答案
怎么样.....
在您的 MemberSerializer 上,定义一个字段,例如:
On your MemberSerializer, define a field on it like:
groups = MembershipSerializer(source='membership_set', many=True)
然后在您的会员序列化程序上,您可以创建:
and then on your membership serializer you can create this:
class MembershipSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.Field(source='group.id')
name = serializers.Field(source='group.name')
class Meta:
model = Membership
fields = ('id', 'name', 'join_date', )
这具有创建序列化值、组的整体效果,该值具有您想要的成员资格作为其来源,然后它使用自定义序列化程序提取您想要显示的位.
That has the overall effect of creating a serialized value, groups, that has as its source the membership you want, and then it uses a custom serializer to pull out the bits you want to display.
正如@bryanph 所评论的,serializers.field
在 DRF 3.0 中被重命名为 serializers.ReadOnlyField
,所以这应该是:
as commented by @bryanph, serializers.field
was renamed to serializers.ReadOnlyField
in DRF 3.0, so this should read:
class MembershipSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.ReadOnlyField(source='group.id')
name = serializers.ReadOnlyField(source='group.name')
class Meta:
model = Membership
fields = ('id', 'name', 'join_date', )
适用于任何现代实现
这篇关于在 Django Rest Framework 的响应中包含中介(通过模型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!