filtering detail results in django rest framework -
i have following models
class customuser(abstractbaseuser, persmissionsmixin): #custom fields practice = models.foreignkey(practice) class customer(models.model): #fields practice = models.foreignkey(practice) class peri(models.model): customer = models.foreignkey(customer)
i want create api peri model, want limit results authenticated users belong same practice customer whom peri belongs to. after creating serializer perimodel created modelviewset it, this
class periviewset(modelviewset): #serializer_class etc def get_queryset(self): user = self.request.user practice = user.practice return peri.objects.filter(customer__practice=practice)
the above code return peri's belong customer has same practice logged in user. this:
http://example.com/api/peri/
will return above filtered queryset. how detail views. detail view of modelviewset re-evaluate queryset? or use preexisting queryset calculated get_queryset
?
what mean if queryset consits of models id [2,5,6,7] , user tries visit following url
http://example.com/api/peri/9/
will results supposing peri id 9 doesn't have same practice logged in user has?does filtering listview queryset apply detailview also? better if used method here under custom generic filtering section??
take @ source code of django-rest-framework (https://github.com/tomchristie/django-rest-framework), @ generics.py module:
def get_object(self): """ returns object view displaying. may want override if need provide non-standard queryset lookups. eg if objects referenced using multiple keyword arguments in url conf. """ queryset = self.filter_queryset(self.get_queryset()) [...]
so, get_object
uses get_queryset
retrieve object. so, filtering get_queryset
enough.
i have notice django-rest-framework great framework need check ultimate truth (source code) find answers questions such yours
Comments
Post a Comment