使用django rest框架自定义lookup_field,无法解析细节

时间:2021-10-20 01:35:16

The error I am getting is

我得到的错误是

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

In my view.py

在我的view.py中

class UserComboViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows UserCombo to be viewed or edited.
    """
    queryset = UserCombo.objects.all()
    serializer_class = UserComboSerializer
    permission_classes = (IsAuthenticated,EmailConfirmationPermission) 
    lookup_field='customURL'

    def get_queryset(self):
        return  UserCombo.objects.filter(user=self.request.user)

In my serializer.py:

在我的serializer.py中:

class UserCombo(serializers.HyperlinkedModelSerializer):
    customURL = serializers.CharField(
       required=False,)

    data = JSONSerializerField()

    class Meta:
        model = UserCombo
        unique_together = (("product", "user"),)
        fields = ('url', 'user', 'product', 'data', 'customURL', 'is_active')
        lookup_field ='customURL'

If I remove the two lookup_field lines everything works fine, but the lookup_field for the usercombo object is the pk.

如果我删除两个lookup_field行,一切正常,但usercombo对象的lookup_field是pk。

1 个解决方案

#1


4  

It looks like using lookup_field was removed as an option for ModelSerializer.Meta in django-rest-framework version 3.2. This error is because of this removal. It should still be possible to use lookup_field with extra_kwargs.

在django-rest-framework版本3.2中,使用lookup_field作为ModelSerializer.Meta的选项被删除了。此错误是由于此删除。仍然可以将lookup_field与extra_kwargs一起使用。

http://www.django-rest-framework.org/topics/3.2-announcement/

#1


4  

It looks like using lookup_field was removed as an option for ModelSerializer.Meta in django-rest-framework version 3.2. This error is because of this removal. It should still be possible to use lookup_field with extra_kwargs.

在django-rest-framework版本3.2中,使用lookup_field作为ModelSerializer.Meta的选项被删除了。此错误是由于此删除。仍然可以将lookup_field与extra_kwargs一起使用。

http://www.django-rest-framework.org/topics/3.2-announcement/