django-tastypie: Loading fields based on request -
i have resources structure in tastypie similar following:
class userresource(modelresource): class meta: queryset = user.objects.all() class activityresource(modelresource): class meta: queryset = activity.objects.all() class useractivityresource(modelresource): user = fields.toonefield(userresource, 'user') activity = fields.toonefield(activityresource, 'activity', full=true) class meta: queryset = useractivity.objects.all() filtering = { 'user': all_with_relations, 'activity': all_with_relations }
it many many objects relation.
sometimes want retrieve data useractivity using specific id object, gives me in result specific fields useractivity + user data related + activity data related, pretty good, example:
get /api/v1/user_activity/{user_activity_id}/
other times want retrieve data useractivity based on, example id of related user object, example:
get /api/v1/user_activity/?user={user_id}
this request works pretty also.
now, let's {user_id} sent in request has 10 related activities (activityresource) in join object. so, when request execution, can notice system running following queries build result:
select count(*) user_activity {filters} (building meta...ok)
and each activity related (10 times):
select user_activity.* user_activity {filters} (getting user_activity data...ok) select activity.* activity {filters} (getting activity data...ok) select user.* user id={user_id} (getting user data...ok?)
the thing is, know user used find related activities, then, don't need query again data, , of course not interested in execute same query 10 times!
so, question is...
is there way optimize process, can activate/deactivate use of related field user depending on request avoid useless queries? or in other words, modify related objects of resource on fly ?
thank you.
btw: i'm using django1.4, django-tastypie 0.9.11
Comments
Post a Comment