I tested the method on Tastypie 0.11 and seems to work ok. But in my opinion you are taking wrong and error prone approach. I prepared solution that will eliminate them.
Problem definition
You didn't defined what is your code supposed to do. Reviewing code I assume you want to have resource that returns object for currently logged user and filtered by day name.
In other words you want to limit access to resource - And this is.. I think you know, authorization.
Why not obj_get_list?
You see that Tastypie obj_get_list
is quite Django ORM specific and not documented well. I don't really know how to use it. I wouldn't dare to deal with it.
Solution
Let's define Authorization class for your resource:
from datetime import datetime
from tastypie.authorization import Authorization
class DriverAuthorization(Authorization):
def read_list(self, object_list, bundle):
todays_day_name = datetime.now().strftime('%w')
return object_list.filter(driver__user=bundle.request.user,
day=todays_day_name)
def read_detail(self, object_list, bundle):
todays_day_name = datetime.now().strftime('%w')
return bundle.obj.day == todays_day_name and bundle.obj.driver.user == bundle.request.user
def create_list(self, object_list, bundle):
return []
def create_detail(self, object_list, bundle):
return False
def update_list(self, object_list, bundle):
return []
def update_detail(self, object_list, bundle):
object_before_update = object_list.get(pk=bundle.obj.pk)
return object_before_update.driver.user == bundle.request.user
def delete_list(self, object_list, bundle):
return []
def delete_detail(self, object_list, bundle):
return False
Now attach authorization to resource:
class DriveResource(CommonModelResource):
driver = fields.ForeignKey(DriverResource, 'driver')
bus = fields.ForeignKey(BusResource, 'bus')
route = fields.ForeignKey(RouteResource, 'route')
class Meta(CommonModelResource.Meta):
queryset = Drive.objects.all()
allowed_methods = ['get', 'put']
excludes = ('bus_location',)
authorization = DriverAuthorization()
filtering = {
'updated': ('gt', 'gte', 'lt', 'lte', 'exact'),
'id': ('exact',),
}
def dehydrate_driver(self, bundle):
return bundle.obj.driver.person_id
def dehydrate_bus(self, bundle):
return bundle.obj.bus.number
def dehydrate_route(self, bundle):
return bundle.obj.route.id
def hydrate_bus_location(self, bundle):
# Guard against the method being called twice
if not hasattr(bundle, 'geopoint_processed'):
lon = bundle.data['bus_location'][0] / 1e6
lat = bundle.data['bus_location'][1] / 1e6
bundle.data['bus_location'] = Point(lon, lat)
bundle.geopoint_processed = True
return bundle
Benefits
1. Code now is much more clearer to read
2. You have control on what user can do or cannot do in one place.
3. No obj_get_list
means no more problems.