簡體   English   中英

```HyperlinkedModelSerializer```中配置不正確的lookup_field錯誤

[英]Incorrectly Configured lookup_field error in ```HyperlinkedModelSerializer```

我自己Dhinesh 但我是Django Rest Framework的初學者。 在這里,我試圖將 lookup_field 從pk更改為自定義字段( IP_Address )。 但是我一次又一次地遇到這個錯誤。 幫助解決這個問題。

ImproperlyConfigured at /api/nodes/192.168.1.200/ Could not resolve URL for hyperlinked relationship using view name "nodemaster-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field. ImproperlyConfigured at /api/nodes/192.168.1.200/ Could not resolve URL for hyperlinked relationship using view name "nodemaster-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.

模型.py

class NodeMaster(models.Model):
    Device_Manufacturer = models.CharField(max_length=50, choices = MANUFACTURER)
    Device_Type = models.CharField(max_length=50, choices = DEVICE_TYPE)
    Physical_Location = models.CharField(max_length= 50, null= True)
    IP_Address =  models.CharField(max_length=50,unique = True )
    Community_String = models.CharField(max_length = 50, null = False)

    def __str__(self):
        return self.IP_Address

序列化程序.py

class NodeSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = NodeMaster
        fields = '__all__'
        lookup_field = "IP_Address"

視圖.py

class NodeViewSet(viewsets.ModelViewSet):
    serializer_class = NodeSerializer
    queryset = NodeMaster.objects.all()
    lookup_field = "IP_Address"

網址.py

node_list = NodeViewSet.as_view({
    'get':'list',
    'post' : 'create'
})

node_detail = NodeViewSet.as_view({
    'get':'retrieve',
    'put' : 'update',
    'patch' : 'partial_update',
    'delete' : 'destroy'
})

urlpatterns = format_suffix_patterns([
    path('', node_list, name = 'nodemaster-list'),
    path('<str:IP_Address>/', node_detail, name='nodemaster-detail')
])

顯然,根據https://www.django-rest-framework.org/community/3.1-announcement/#deprecations中的 DjangoRestframework 文檔,lookup_field 已被刪除,但他們建議為此使用 extra_kwargs。 否則,只需刪除 lookup_field 即可刪除我的代碼的錯誤。

暫無
暫無

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

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