Opened 17 months ago

Last modified 6 months ago

#19431 assigned New feature

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 17 months ago.

Download all attachments as: .zip

Change History (8)

Changed 17 months ago by aleray

comment:1 Changed 16 months ago by apollo13

  • Has patch unset
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 16 months ago by pyriku

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

comment:3 Changed 15 months ago by slurms

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

comment:4 Changed 11 months ago by ptone

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 11 months 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 11 months ago by pyriku

  • Owner changed from nobody to pyriku
  • Status changed from new to assigned

comment:7 Changed 6 months ago by timo

  • Easy pickings unset
  • Needs tests unset

Add Comment

Modify Ticket

Change Properties
<Author field>
as assigned
The owner will be changed from pyriku to anonymous. Next status will be 'assigned'
The ticket will be disowned. Next status will be 'new'
as The resolution will be set. Next status will be 'closed'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.