簡體   English   中英

Django Rest Framework - AssertionError 修復您的 URL conf,或正確設置視圖上的 `.lookup_field` 屬性

[英]Django Rest Framework - AssertionError Fix your URL conf, or set the `.lookup_field` attribute on the view correctly

我正在嘗試作為特定於用戶的單個對象(而不是查詢集)返回,而無需在請求的 URL 中指定標識符/pk。 每個用戶都有一個組織 FK。

http://website/organisation而不是http://website/organisation/1

我收到以下錯誤,因為它期待此標識符: AssertionError: Expected view OrganisationDetail to be called with a URL keyword argument named "user__organisation_id". Fix your URL conf, or set the AssertionError: Expected view OrganisationDetail to be called with a URL keyword argument named "user__organisation_id". Fix your URL conf, or set the attribute on the view correctly. AssertionError: Expected view OrganisationDetail to be called with a URL keyword argument named "user__organisation_id". Fix your URL conf, or set the attribute on the view correctly. .lookup_field attribute on the view correctly.

使用 RetrieveModelMixin/GenericAPIView 時如何/我需要指定什么,以便它返回由 FK 鏈接的單一對象?

我的視圖類:

class OrganisationDetail(mixins.RetrieveModelMixin, mixins.UpdateModelMixin,generics.GenericAPIView):
    serializer_class = OrganisationDetailSerializer
    lookup_field = 'pk' #yes, I know this is the default and there's no need to speciy

    def get_queryset(self):
        return Organisation.objects.filter(pk=self.request.user.organisation_id)

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

相關網址: url(r'^api/v1/organisation/$', OrganisationDetail.as_view()),

我的型號:

class Person(AbstractUser):
    organisation = models.ForeignKey(Organisation, related_name='members', null=True)
    is_admin = models.BooleanField(default=False)
    def __str__(self):
        return self.first_name + " " + self.last_name + " - " + self.email

對於詳細視圖,您需要覆蓋get_object() ,而不是get_queryset() 您仍然需要權限檢查,所以我建議查看源代碼。 首先刪除你的get_queryset()方法,然后為初學者嘗試這個:

# inside OrganisationDetail
queryset = Organisation.objects.all()

def get_object(self):
    queryset = self.filter_queryset(self.get_queryset())
    # make sure to catch 404's below
    obj = queryset.get(pk=self.request.user.organisation_id)
    self.check_object_permissions(self.request, obj)
    return obj

如果您使用的是 mixins,請將 lookup_field 更改為正確的

class OrganisationDetail(mixins.RetrieveModelMixin, mixins.UpdateModelMixin,generics.GenericAPIView):

    serializer_class = OrganisationDetailSerializer
    lookup_field = 'members' 

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def patch(self, request, *args, **kwargs):
        return self.partial_update(request, *args, **kwargs)

我的解決方案是更改一個參數,它有 id,而不是應該只顯示一個元素的路徑中的 pk,當將其更改為 <int: pk> 時,它運行得非常好

from the django.urls import path from rest_framework.urlpatterns import format_suffix_patterns from posts import views

urlpatterns = [path('posts/',views.PublicationList.as_view()),path('posts/<int:id>/',views.Publication_detail.as_vi enter code here ew()),]

urlpatterns = format_suffix_patterns (urlpatterns)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM