问题
I need to access the HttpRequest object in my Resource's dehydrate method.
In the docs, it shows that bundle.request is a valid attribute (it's in the resources.html page). When I try to add it to my code, I get an error claiming that Bundle' object has no attribute 'request'. What gives?
回答1:
I just had the same problem, but found the correct answer over here: http://groups.google.com/group/django-tastypie/tree/browse_frm/thread/801f44af3f2dbe7b/a36f303380eacf96
it seems django-tasty-pie version 0.9.9 didn't have this attribute but version 0.9.10 does!
so if you use buildout, look in to buildout.cfg
under versions:
search for django-tastypie = 0.9.9
remove this one and see what your install picks or replace it with:
django-tastypie = 0.9.10
I still have this problem, so opened a new link, see:
django-tastypie: Cannot access bundle.request in dehydrate(self,bundle)
in the question above I found out, that using 0.9.10 is not enough, version 1.0.0 beta should do the tric..
回答2:
Bundle object has request attribute.
class Bundle(object):
"""
A small container for instances and converted data for the
``dehydrate/hydrate`` cycle.
Necessary because the ``dehydrate/hydrate`` cycle needs to access data at
different points.
"""
def __init__(self, obj=None, data=None, request=None):
self.obj = obj
self.data = data or {}
self.request = request or HttpRequest()
Anyway, you can ovveride the Resource method higher than dehydrate
in the call stack.
Could you show the code?
来源:https://stackoverflow.com/questions/7389632/django-tastypie-how-do-you-access-httprequest-object-in-the-bundle