Opened 4 years ago

Closed 20 months 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



I wanted to prepopulate admin form datetime fields using querystrings like this:


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/, adding a special case for datetimefield. Attached is a very simple patch, let me know what you think...



Attachments (1)

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

Download all attachments as: .zip

Change History (9)

Changed 4 years ago by aleray

comment:1 Changed 4 years ago by Florian Apolloner

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 Changed 4 years ago by pyriku

Implementation without using dateutil and using DATETIME_INPUT_FORMATS submitted on this pull request:

comment:3 Changed 4 years ago by Nick Sandford

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

comment:4 Changed 4 years ago by Preston Holmes

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 Changed 4 years ago by pyriku

Just updated the pull request using ISO format instead:

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 Changed 4 years ago by pyriku

Owner: changed from nobody to pyriku
Status: newassigned

comment:7 Changed 3 years ago by Tim Graham

Easy pickings: unset
Needs tests: unset

comment:8 Changed 20 months ago by Tim Graham

Resolution: duplicate
Status: assignedclosed

Duplicate of #9739

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