Opened 13 years ago
Closed 13 years ago
#15950 closed New feature (duplicate)
Allow ChoiceField and friends to take a callable for choices kwarg
Reported by: | Jeremy Dunck | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 1.3 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Sometimes we'd like choices to be dynamic (often the result of an ORM query), but because models are declarative, this forces queries to run with Django partially loaded.
It would be good to be able to make choices a callable so that these queries could be deferred until Django is fully loaded.
I'm attaching a rough patch; if this passes design-decision, I can polish it up with tests.
Attachments (1)
Change History (4)
by , 13 years ago
Attachment: | callable-choice.diff added |
---|
comment:1 by , 13 years ago
Component: | Uncategorized → Forms |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 13 years ago
Hmm, writing a generator to do a query only on the first execution of the generator is a little fiddly. (If there's a nice way to write it, please give an example.)
There's a common pattern of accepting callables - maybe we should add callable to model choices, too?
comment:3 by , 13 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
This was actually already suggested in #13181.
Yeah, seems reasonable, although I'd prefer something like what the model fields do, taking any iterable instead:
http://code.djangoproject.com/browser/django/trunk/django/db/models/fields/__init__.py?rev=15982#L393
Would that solve the issue for you, too?