Django Rest Framework 实现多对多关系
Django Rest Framework implementing many to many relationship
我想实现一种 many to many
关系,这样一个人可以加入多个社交俱乐部,一个社交俱乐部可以有多个成员。这是我得到的结果,但是它没有显示电影的详细信息,序列化给我一个错误 -
Original exception text was: 'ManyRelatedManager' object has no attribute 'club_name'.
models.py
class Club(models.Model):
club_name = models.CharField(default='',blank=False,max_length=100)
class Person(models.Model):
person_name = models.CharField(default='',blank=False,max_length=200)
clubs = models.ManyToManyField(Club)
serializers.py
class ClubSerializer(serializers.ModelSerializer):
class Meta:
model = Club
fields = ('url','id','club_name','person')
class PersonSerializer(serializers.ModelSerializer):
clubs = ClubSerializer()
class Meta:
model = Person
fields = ('url','id','person_name','clubs')
views.py
class ClubDetail(generics.ListCreateAPIView):
serializer_class = ClubSerializer
def get_queryset(self):
club = Clubs.objects.get(pk=self.kwargs.get('pk',None))
persons = Person.objects.filter(club=club)
return persons
class ClubList(generics.ListCreateAPIView):
queryset = Club.objects.all()
serializer_class = ClubSerializer
class PersonDetail(generics.RetrieveUpdateDestroyAPIView):
serializer_class = PersonSerializer
def get_object(self):
person_id = self.kwargs.get('pk',None)
return Person.objects.get(pk=person_id)
urls.py
urlpatterns = format_suffix_patterns([
url(r'^$', views.api_root),
url(r'^clubs/$',
views.ClubList.as_view(),
name='club-list'),
url(r'^clubs/(?P<pk>[0-9]+)/persons/$',
views.ClubDetail.as_view(),
name='club-detail'),
url(r'^person/(?P<pk>[0-9]+)/$',
views.PersonDetail.as_view(),
name='person-detail'),
])
在我的本地服务器上,我得到了俱乐部列表,该列表将我导航到俱乐部详细信息视图,但它不显示电影名称,或者,但我看不到人员列表。我该如何正确执行此操作?
您的代码中存在许多问题。即:
- ClubSerializer.Meta.fields - 人应该是 person_set
- PersonSerializer - 俱乐部应该是
clubs = ClubSerializer(many=True)
- ClubDetail.serializer_class - ClubSerializer 应该是 PersonSerializer
- ClubDetail - 查询集应该是
persons = Person.objects.filter(clubs=club)
如果您解决了上述问题,您应该能够导航至 ClubDetail
并查看俱乐部成员列表。
如果您使用的是多对多关系,请确保显式声明该字段,如下所示:
class WidgetSerializer(serializers.HyperlinkedModelSerializer):
parts__part = serializers.CharField()
class Meta:
model = Widget
fields = ['date', 'url', 'type','parts__part']
我想实现一种 many to many
关系,这样一个人可以加入多个社交俱乐部,一个社交俱乐部可以有多个成员。这是我得到的结果,但是它没有显示电影的详细信息,序列化给我一个错误 -
Original exception text was: 'ManyRelatedManager' object has no attribute 'club_name'.
models.py
class Club(models.Model):
club_name = models.CharField(default='',blank=False,max_length=100)
class Person(models.Model):
person_name = models.CharField(default='',blank=False,max_length=200)
clubs = models.ManyToManyField(Club)
serializers.py
class ClubSerializer(serializers.ModelSerializer):
class Meta:
model = Club
fields = ('url','id','club_name','person')
class PersonSerializer(serializers.ModelSerializer):
clubs = ClubSerializer()
class Meta:
model = Person
fields = ('url','id','person_name','clubs')
views.py
class ClubDetail(generics.ListCreateAPIView):
serializer_class = ClubSerializer
def get_queryset(self):
club = Clubs.objects.get(pk=self.kwargs.get('pk',None))
persons = Person.objects.filter(club=club)
return persons
class ClubList(generics.ListCreateAPIView):
queryset = Club.objects.all()
serializer_class = ClubSerializer
class PersonDetail(generics.RetrieveUpdateDestroyAPIView):
serializer_class = PersonSerializer
def get_object(self):
person_id = self.kwargs.get('pk',None)
return Person.objects.get(pk=person_id)
urls.py
urlpatterns = format_suffix_patterns([
url(r'^$', views.api_root),
url(r'^clubs/$',
views.ClubList.as_view(),
name='club-list'),
url(r'^clubs/(?P<pk>[0-9]+)/persons/$',
views.ClubDetail.as_view(),
name='club-detail'),
url(r'^person/(?P<pk>[0-9]+)/$',
views.PersonDetail.as_view(),
name='person-detail'),
])
在我的本地服务器上,我得到了俱乐部列表,该列表将我导航到俱乐部详细信息视图,但它不显示电影名称,或者,但我看不到人员列表。我该如何正确执行此操作?
您的代码中存在许多问题。即:
- ClubSerializer.Meta.fields - 人应该是 person_set
- PersonSerializer - 俱乐部应该是
clubs = ClubSerializer(many=True)
- ClubDetail.serializer_class - ClubSerializer 应该是 PersonSerializer
- ClubDetail - 查询集应该是
persons = Person.objects.filter(clubs=club)
如果您解决了上述问题,您应该能够导航至 ClubDetail
并查看俱乐部成员列表。
如果您使用的是多对多关系,请确保显式声明该字段,如下所示:
class WidgetSerializer(serializers.HyperlinkedModelSerializer):
parts__part = serializers.CharField()
class Meta:
model = Widget
fields = ['date', 'url', 'type','parts__part']