Opened 11 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)

modelform_qs_initial_datetime.patch (887 bytes ) - added by aleray 11 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 by Florian Apolloner, 11 years ago

Has patch: unset
Triage Stage: UnreviewedAccepted

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.

comment:2 by pyriku, 11 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 Nick Sandford, 11 years ago

Has patch: set
Needs tests: set
Patch needs improvement: set

comment:4 by Preston Holmes, 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 pyriku, 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 pyriku, 11 years ago

Owner: changed from nobody to pyriku
Status: newassigned

comment:7 by Tim Graham, 11 years ago

Easy pickings: unset
Needs tests: unset

comment:8 by Tim Graham, 9 years ago

Resolution: duplicate
Status: assignedclosed

Duplicate of #9739

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