Opened 14 years ago

Closed 11 years ago

Last modified 11 years ago

#13972 closed New feature (wontfix)

Field initial value callable should take a request object as an argument

Reported by: Mitar Owned by: nobody
Component: Forms Version: 1.2
Severity: Normal Keywords:
Cc: mmitar@… Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently it is possible to define initial value for a field as a callable. But this is quite limited in use as it does not take any context in which it should run.

I am proposing that it should take a request object. In this way it is possible to set initial values based on a request. For example, based on GeoIP information. We should pass a request object to the form constructor which would then pass it along to the those callables.

I am attaching a short helper function which allows me that I define such callables already. It postprocesses the form class so that request is available to callables. So in the view it is necessary just to construct your form with:

form = initial_accepts_request(request, MyForm)()

It still allows initial values to be passed at construction time and they override callables.

But it would be much better if this would be officially supported.

Attachments (1)

form_utils.py (728 bytes ) - added by Mitar 14 years ago.
A function wich allows callables access to the request

Download all attachments as: .zip

Change History (10)

by Mitar, 14 years ago

Attachment: form_utils.py added

A function wich allows callables access to the request

comment:1 by Mitar, 14 years ago

An example how this can be useful can be found in #5446.

comment:2 by Matthias Kestenholz, 13 years ago

Triage Stage: UnreviewedDesign decision needed

The form classes do not know anything about requests currently, and the prevailing opinion is that this should not be changed in the future.

The last time that coupling requests and forms came up was during the last refactoring of CSRF protection. It was rejected.

This ticket is probably a candidate for "wontfix", this is to be decided by the core developers though.

comment:3 by Graham King, 13 years ago

Severity: Normal
Type: New feature

comment:4 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:5 by Aymeric Augustin, 12 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:6 by Florian Apolloner, 11 years ago

Resolution: wontfix
Status: newclosed

Jupp, wontfix, forms shouldn't have to know about the request.

comment:7 by Mitar, 11 years ago

But how can you then enable forms to have default values based on the user? For example, that default language is set based on his or her IP?

comment:8 by Simon Charette, 11 years ago

You should have this initial_data_for_user logic in your views and pass it along to your form when creating it.

See the getting help section if you need some support.

comment:9 by Mitar, 11 years ago

I know that I can do this in the view. But I find this not a nice separation programming logic. Then I have to repeat again and again this in all the views, where it could just be a nature of the view. Yes, I could create a mixin for class-based views to handle this, but it is still complicating things. request should really be available everywhere. Like there is a way (processors) to get it into the templates, there should be a way to get it into forms.

Maybe better way would be that default values functions could be methods of form? So then you would access to self where you could store anything you want (or pass request in the constructor).

Note: See TracTickets for help on using tickets.
Back to Top