Opened 12 years ago
Closed 9 years ago
#19431 closed New feature (duplicate)
Prepopulate modeladmin forms datetime fields using querystrings
Reported by: | aleray | Owned by: | pyriku |
---|---|---|---|
Component: | contrib.admin | Version: | 1.4 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
Hi,
I wanted to prepopulate admin form datetime fields using querystrings like this:
http://localhost:8000/admin/app/model/add/?start=2013-06-01T20:00:00
But django wasn't really happy with me trying to pass a unicode object where it was expecting a datetime object:
AttributeError at /admin/app/model/add/ 'unicode' object has no attribute 'date'
I came to editing contrib/admin/options.py, adding a special case for datetimefield. Attached is a very simple patch, let me know what you think...
Thanks,
Alex
Attachments (1)
Change History (9)
by , 12 years ago
Attachment: | modelform_qs_initial_datetime.patch added |
---|
comment:1 by , 12 years ago
Has patch: | unset |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 12 years ago
Implementation without using dateutil and using DATETIME_INPUT_FORMATS submitted on this pull request: https://github.com/django/django/pull/589
comment:3 by , 12 years ago
Has patch: | set |
---|---|
Needs tests: | set |
Patch needs improvement: | set |
comment:4 by , 11 years ago
I'd make the comment that because the querystring is presumably being generated by some code, and not a locale-based human form, that we simplify this by just using a standardized ISO format.
But a broader and stickier question is how and where would you document this? This is adding API, and I would argue adding partial API probably to the wrong place.
We have a rich system for the POST side of things when something is submitted, where you can provide your own ModelForm and all its associated validation.
We have a special case for M2M on the GET/form-creation side because of #7738, but that doesn't mean we should have a public way of handling ModelForm initial data from a querystring generally, should we support all field types?
The current, not super friendly, way of handling this would be to preprocess the initial values inside your ModelForm's init - perhaps using the formfield's widget's _format_value method, which is what ModelForms do for POST values.
I'm just not sure this kind of value prep work belongs in ModelAdmin.add_view
comment:5 by , 11 years ago
Just updated the pull request using ISO format instead: https://github.com/django/django/pull/589
I can see your point ptone, but I think that adding those extra checks into the current logic (inside ModelAdmin.add_view
) makes sense, even if we move the entire chunk of logic for prepopulate fields somewhere else eventually. Also, as I said on the PR, I'm not really sure where to add the documentation for this.
comment:6 by , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:7 by , 11 years ago
Easy pickings: | unset |
---|---|
Needs tests: | unset |
Makes sense to me since we have explicit support for M2M field too, so why not DatetimeFields. Using dateutil is not an option though since we don't want a hard dependency there.