#33732: Add request objects as optional input to BaseFrom class
-----------------------------------------+-------------------------------
Reporter: Douglas Mumme | Owner: Douglas Mumme
Type: New feature | Status: assigned
Component: Forms | Version: 4.0
Severity: Normal | Keywords: Froms
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-----------------------------------------+-------------------------------
Hey, I small feature request to make working with forms a little easier.
Which is basically to add the request objects as an optional input to the
BaseForm class. I always have to manually set and grab the request input
whenever I want to do validation or set querysets that involve the
request.user. It's a small thing, but just think it would be a cleaner way
to get the request object in the form as I have needed to do that and I'm
sure others have as well a lot.
basically something like this altering the __init__ of the BaseForm class
where I just added the request input and set it.
{{{
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
initial=None, error_class=ErrorList, label_suffix=None,
empty_permitted=False, field_order=None,
use_required_attribute=None,
renderer=None, request=None):
self.is_bound = data is not None or files is not None
self.data = MultiValueDict() if data is None else data
self.files = MultiValueDict() if files is None else files
self.auto_id = auto_id
if prefix is not None:
self.prefix = prefix
self.initial = initial or {}
self.error_class = error_class
# Translators: This is the default suffix added to form field
labels
self.label_suffix = label_suffix if label_suffix is not None else
_(':')
self.empty_permitted = empty_permitted
self._errors = None # Stores the errors after clean() has been
called.
# The base_fields class attribute is the *class-wide* definition
of
# fields. Because a particular *instance* of the class might want
to
# alter self.fields, we create self.fields here by copying
base_fields.
# Instances should always modify self.fields; they should not
modify
# self.base_fields.
self.fields = copy.deepcopy(self.base_fields)
self._bound_fields_cache = {}
self.order_fields(self.field_order if field_order is None else
field_order)
if use_required_attribute is not None:
self.use_required_attribute = use_required_attribute
if self.empty_permitted and self.use_required_attribute:
raise ValueError(
'The empty_permitted and use_required_attribute arguments
may '
'not both be True.'
)
# Initialize form renderer. Use a global default if not specified
# either as an argument or as self.default_renderer.
if renderer is None:
if self.default_renderer is None:
renderer = get_default_renderer()
else:
renderer = self.default_renderer
if isinstance(self.default_renderer, type):
renderer = renderer()
self.renderer = renderer
self.request = request
}}}
I'm not sure if there are any reasons why this hasn't been done before,
but please let me know if this is a possiblity. Thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/33732>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070180ed51ef3f-920e0af2-b860-4e8c-a888-2e24d4dada25-000000%40eu-central-1.amazonses.com.