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!
Duplicate of #26248. To reconsider a wontfix decision (or for design changes like this), you can write to the DevelopersMailingList to reach a wider audience and try to get a consensus.