Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom lookup_field with django rest framework, Cannot resolve detail

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

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:

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.

like image 964
mike29892 Avatar asked Sep 10 '15 19:09

mike29892


1 Answers

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.

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

like image 190
mike29892 Avatar answered Nov 02 '22 09:11

mike29892