RequestFetchingMixin
More a proof of concept than anything, this mixin addresses a perceived "shortcoming" of django forms: the inability to interact with the current request, and specifically the "current user". Usage: class SomeForm(forms.Form, RequestFetchingMixin): # fields... def __init__(self, *args, **kwargs): if self.request: # interact with self.request? super(SomeForm, self).__init__(*args, **kwargs) if self.request: # more interaction def clean(self): if self.request: # blah blah self.request.user blah Notes: * This reaches "into the past" to pull an existing HttpRequest object, regardless of what name it was bound to, into "the present". * If multiple requests are found (what on earth are you doing??), the one bound to "request" (or the first one found, if that's not available) will be used. * By default it goes up to 5 frames back before giving up, but that can obviously be changed if you think you need it. * This won't work everywhere. self.request is guaranteed to be present, but may be None. * Just because you have a self.request doesn't mean self.request.user will be a valid user. This is subject to all the rules that a regular view's request is subject to (because it *is* a regular view's request!) * This isn't magic; it's just python.
- forms
- request
- mixin
- request.user