Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails with custom lookup_field #3

Open
hroncok opened this issue May 3, 2016 · 1 comment
Open

Fails with custom lookup_field #3

hroncok opened this issue May 3, 2016 · 1 comment

Comments

@hroncok
Copy link

hroncok commented May 3, 2016

I have the following setup:

# Course model has sport as related filed to Sport model with it's ID

# serializers.py:
from drf_hal_json import serializers

class SportSerializer(serializers.HalModelSerializer):
    class Meta:
        model = models.Sport
        fields = ('shortcut',)

class CourseSerializer(serializers.HalModelSerializer):
    class Meta:
        model = models.Course
        fields = ('sport',)

# views.py:
class SportViewSet(*base):
    queryset = models.Sport.objects.all()
    serializer_class = serializers.SportSerializer

class CourseViewSet(*base):
    queryset = models.Course.objects.all()
    serializer_class = serializers.CourseSerializer

Now it all works fine but I want to use custom lookup_field on sports:

# Course model has sport as related filed to Sport model with it's ID

# serializers.py:
class SportSerializer(serializers.HalModelSerializer):
    url = serializers.HyperlinkedIdentityField(
         read_only=True, view_name='sport-detail', lookup_field='shortcut')
    # ...

class CourseSerializer(serializers.HalModelSerializer):
    sport = serializers.HyperlinkedRelatedField(
         read_only=True, view_name='sport-detail', lookup_field='shortcut')
    # ...

# views.py:
class SportViewSet(*base):
    # ...
    lookup_field = 'shortcut'

It doesn't work, resulting in exception:

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

But when I use HyperlinkedModelSerializer from DRF, it works fine, so I suspect the problem is in HalModelSerializer.

I don't really understand HalModelSerializer's code, but it seems it completely redefines the fields, thus the information I provided within HyperlinkedRelatedField constructor gets lost.

I tried to set view_name='balahblah-detail' just to see what happens and the exception still talks about sport-detail which I think backs up my thought.

BTW see this commit and the code there hroncok/utvsapi-django@8e9f793 for complete code example.

And thanks for drf-hal-json.

@LordShion
Copy link

Hi I'm trying to get drf-hal-json working, and I get the same issue.
serializers.ModelSerializer acts ok , data is retrieved with pagination
but as soon as I use the HalModelSerializer I get this same issue.
any Idea How to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants