You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Course model has sport as related filed to Sport model with it's ID# serializers.py:fromdrf_hal_jsonimportserializersclassSportSerializer(serializers.HalModelSerializer):
classMeta:
model=models.Sportfields= ('shortcut',)
classCourseSerializer(serializers.HalModelSerializer):
classMeta:
model=models.Coursefields= ('sport',)
# views.py:classSportViewSet(*base):
queryset=models.Sport.objects.all()
serializer_class=serializers.SportSerializerclassCourseViewSet(*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:classSportSerializer(serializers.HalModelSerializer):
url=serializers.HyperlinkedIdentityField(
read_only=True, view_name='sport-detail', lookup_field='shortcut')
# ...classCourseSerializer(serializers.HalModelSerializer):
sport=serializers.HyperlinkedRelatedField(
read_only=True, view_name='sport-detail', lookup_field='shortcut')
# ...# views.py:classSportViewSet(*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.
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?
I have the following setup:
Now it all works fine but I want to use custom lookup_field on sports:
It doesn't work, resulting in exception:
But when I use
HyperlinkedModelSerializer
from DRF, it works fine, so I suspect the problem is inHalModelSerializer
.I don't really understand
HalModelSerializer
's code, but it seems it completely redefines the fields, thus the information I provided withinHyperlinkedRelatedField
constructor gets lost.I tried to set
view_name='balahblah-detail'
just to see what happens and the exception still talks aboutsport-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.
The text was updated successfully, but these errors were encountered: