python - Django REST Framework hyperlink URL failing to resolve -
i trying set hyperlinking in django rest framework api, , life of me can't find out error is.
my model:
class franchise(models.model): id = models.autofield(primary_key=true) name = models.charfield(max_length=255) # other fields
my serializer
class franchiselistserializer(serializers.hyperlinkedmodelserializer): url = serializers.hyperlinkedidentityfield( view_name='franchise_details', lookup_field='id', lookup_url_kwarg='franchiseid' ) class meta: model = franchise fields = ('id', 'name', 'url')
my urls:
url(r'^db/franchise/$', views.franchise_index, name='db_franchise_index'), url(r'^db/franchise/(?p<franchiseid>[0-9]+)/$', views.franchise_details, name='db_franchise_details')
note included url conf, api functionality goes within /api/ url
my views:
@api_view(['get']) def franchise_index(request, format=none): franchise_list = franchise.objects.all() serializer = franchiselistserializer(franchise_list, many=true, context={'request': request}) return response(serializer.data) @api_view(['get']) def franchise_details(request, franchiseid, format=none): franchise = franchise.objects.get(id=franchiseid) serializer = franshisedetailserializer(franchise) return response(serializer.data)
note franshisedetailserializer
seen above works fine.
summary:
- url /api/db/franchise/ goes view
franchise_index
, returns data serializedfranchiselistserializer
. - url /api/db/franchise/
<franchiseid>
/ goes viewfranchise_details
, returns data serializedfranchisedetailserializer
(works fine)
as can see, have added url field franchiselistserializer
, supposed link corresponding franchise details page. before added url field, serializer returned id
, name
, correct , expected behaviour @ time.
when go /api/db/franchise/ now, error:
improperlyconfigured @ /api/db/franchise/ not resolve url hyperlinked relationship using view name "franchise_details". may have failed include related model in api, or incorrectly configured `lookup_field` attribute on field.
following hint, set arguments in url
field of franchiselistserializer
, , far can tell, correct. have checked , double checked drf documentation, here, here , here, have found no solution.
following other similar issues on stackoverflow, tried changing view_name='franchise_details'
view_name='api:franchise_details'
(the name of django app relevant files in) , view_name='api:franchise_details-detail'
, no avail.
any , appreciated, cheers.
thanks @aks's promting, figured out. view_name
name of url, not view. way read documentation (and fact view_name
not url_name
), seemed supposed name of view.
i had tried using view_name='db_franchise_details'
(my urls name) before, did not work. after aks promted comment, tried again, , tried using view_name='api:db_franchise_details'
, work!
Comments
Post a Comment