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
Widget for editing CommaSeparatedIntegerField with a set of checkboxes. Each checkbox corresponds to single integer value. So, choices should be pre-defined for widget.
**Example**
Assume, we need a registration form for some event with time frame for 3 days, starting at Monday. User can select any of 3 days, so we need to show 3 checkboxes.
Here's the basic example for such form:
class EventRegistrationForm(forms.Form):
days = forms.CharField(widget=NumbersSelectionWidget(
['Mon', 'Tue', 'Wed'], range(1, 4)))
- forms
- widget
- comma-separated