python - Django REST framework - HyperlinkedRelatedField with additional parameter -
i'm building rest web api django rest framework. have many-to-many relation between categories , wallets. starting category retrieve wallets linked to, , shell works fine
models:
class wallet(models.model): nome = models.charfield(max_length=255) creato = models.datetimefield(auto_now=false) utente = models.foreignkey(user) wallettype = models.foreignkey(wallettype) payway = models.manytomanyfield(payway, through = 'wpw', blank = true) defaultpayway = models.foreignkey('wpw', related_name = 'def_pw', null = true) def __unicode__(self): return self.nome class category(models.model): nome = models.charfield(max_length=255) creato = models.datetimefield(auto_now=false) enus = models.foreignkey(enus) wallet = models.manytomanyfield(wallet) utente = models.foreignkey(user) owner = models.manytomanyfield(user, related_name='owner_cat')
urls:
urlpatterns = patterns( '', url(r'^$', views.cat_list.as_view()), url(r'^/(?p<pk>[0-9]+)/*$', views.cat_detail.as_view()), url(r'^/(?p<cat_id>[0-9]+)/wallets/*$', views.cwallet_list.as_view()), url(r'^/(?p<cat_id>[0-9]+)/wallets/(?p<pk>[0-9]+)/*$', views.cwallet_detail.as_view(), name='cwallet-detail'), )
serializer:
class cat_serializer(serializers.modelserializer): wallet = serializers.hyperlinkedrelatedfield( many=true, read_only=true, view_name='cwallet-detail', # lookup_field = 'pk', # lookup_url_kwarg = 'pk' ) subcat_count = serializers.integerfield( source='subcategory_set.count', read_only=true ) class meta: model = category fields = ('id', 'nome','wallet','subcat_count')
when call:
get http://localhost:8000/categories/77/wallets
i retrieve:
{ 'nome': 'dinner', 'subcat_count': 12, 'wallet': { 'http://localhost:8000/77/wallet/1', 'http://localhost:8000/77/wallet/2', 'http://localhost:8000/77/wallet/3', } }
but doesn't work , error:
"could not resolve url hyperlinked relationship using view name "cwallet-detail". may have failed include related model in api, or incorrectly configured lookup_field
attribute on field."
i think problem linked additional param: in fact in urls.py have pk wallet id , cat_id category id. can't figure how pass category id 'cwallet-detail'.
anyone know how pass second paramater hyperlinkedrelatedfield?
thank in advance.
after reading docs , posting new issue on tom christie's django rest github, know it's not officially supported, doumentations , patch work in progress.
actually there this link building custom hyperlinked filed solve problem.
hope has same problem.
Comments
Post a Comment