Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#33732 closed New feature (duplicate)

Add request object as optional input to BaseForm class

Reported by: Douglas Mumme Owned by: Douglas Mumme
Component: Forms Version: 4.0
Severity: Normal Keywords: Froms
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

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!

Change History (4)

comment:1 by Douglas Mumme, 2 years ago

Summary: Add request objects as optional input to BaseFrom classAdd request object as optional input to BaseFrom class

comment:2 by Tim Graham, 2 years ago

Resolution: duplicate
Status: assignedclosed

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.

comment:3 by Douglas Mumme, 2 years ago

Sounds good and will do. thank you!

comment:4 by Douglas Mumme, 2 years ago

Summary: Add request object as optional input to BaseFrom classAdd request object as optional input to BaseForm class
Note: See TracTickets for help on using tickets.
Back to Top