4
我正在用tastypie爲基於用戶的數據創建一個django應用程序的API。資源是這樣的:如何通過request.user過濾django-tastypie的ToManyField?
class PizzaResource(ModelResource):
toppings = fields.ToManyField(
'project.app.api.ToppingResource',
'topping_set'
)
class Meta:
authentication = SessionAuthentication()
queryset = Pizza.objects.all()
def apply_authorization_limits(self, request, object_list):
return object_list.filter(users=request.user)
class ToppingResource(ModelResource):
pizza = fields.ForeignKey(PizzaResource, 'pizza')
class Meta:
authentication = SessionAuthentication()
queryset = Topping.objects.filter()
的根據模型是這樣的:
class Pizza(model):
users = ManyToManyField(User)
toppings = ManyToManyField(Topping)
# other stuff
class Topping(Model):
used_by = ManyToManyField(User)
# other stuff
現在我想要做的就是篩選列出pizza
由Topping.used_by
場toppings
。我剛剛找到了how to filter this field by request unrelated data。
如何通過請求數據篩選tastypie
的關係字段?
任何方式來做到這一點,但它不會查詢所有的對象?在我執行prefetch_related之前......例如在我的查詢集中,我正在執行一個prefetch_related('topping_set'),這意味着對所有對象進行一次查詢。現在,但是它爲每個返回的對象進行查詢... – abisson