我有2个模型。
Account:
id, name, age, discoverable
Friendship:
id, requestor, acceptor (requestor and acceptor are Account IDs)
我需要编写2个api。
获取所有具有Discoverable = True的帐户。我通过在AccountViewSet中的查询集上应用过滤器来完成此操作,
def filter_queryset(self, queryset):
queryset = queryset.filter(discoverable=True).all()
return queryset
使用以下URL方案<server-url>/accounts/1
获取帐户详细信息,其中1是帐户ID。此api应该返回:
答案 0 :(得分:0)
我能够通过如下重写AccountViewSet的get_object()方法来实现它。
def get_object(self, queryset=None):
"""
Fix - If account has already sent a friend request, show it even if user set
discoverable = False
"""
pk = self.kwargs.get('pk', None)
account = Account.objects.filter(id=pk).first()
if account:
if account.is_friends_with(self.request.user, True):
self.check_object_permissions(self.request, account)
return account
#default handling in case they are not friends
return super(viewsets.GenericViewSet, self).get_object()