Opened 8 years ago

Last modified 19 months ago

#9739 assigned Bug

Admin does not correctly prefill DataTimeField from URL

Reported by: gilhad Owned by: Ridley Larsen
Component: contrib.admin Version: 1.0
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

I was not able to format URL for Admin interface to prefill DateTimeField with given value.

It worked in 0.96, but does not work in 1.0 ( I used ../admin/MyApp/MyTable/add/?box=359&datum_date=2008-12-01&datum_time=17:30:27)

After some looking on source code and testing i find a solution:

  • in /django/contrib/admin/options.py before line 520 add
      if isinstance(f, models.DateTimeField):
             initial[k] = initial[k].split(",")
    

For reference - the model of MyTable is such :

class MyTable(models.Model):
        box = models.ForeignKey(Boxes)
        datum =  models.DateTimeField(null=True, blank=True)

(plus some other insignificant fields.
The "datum" field should be prefilled with some date, which is computed by long way (not simple now()) and the use must be able to edit it BEFORE saving it)


The problem arises from DateTimeField be treated by MultiWidget, but not properly broken when got by URL (GET)


Patch:

--- options.py.old      2008-12-01 19:56:34.000000000 +0100
+++ options.py  2008-12-01 19:40:34.000000000 +0100
@@ -517,6 +517,8 @@
                     continue
                 if isinstance(f, models.ManyToManyField):
                     initial[k] = initial[k].split(",")
+                if isinstance(f, models.DateTimeField):
+                    initial[k] = initial[k].split(",")
             form = ModelForm(initial=initial)
             for FormSet in self.get_formsets(request):
                 formset = FormSet(instance=self.model())

Attachments (4)

AdminDateTimePatch.diff (1.2 KB) - added by gilhad 8 years ago.
9739-r16345.diff (3.4 KB) - added by Alexander Herrmann 6 years ago.
ticket9737.diff (3.4 KB) - added by Gandalfar 5 years ago.
patch against r17364
patch9739.diff (3.3 KB) - added by Ridley Larsen 19 months ago.
patch against a2e56db

Download all attachments as: .zip

Change History (26)

comment:1 Changed 8 years ago by gilhad

Typo - (Admin does NOT correctly prefill DataTimeField from URL (GET) + patch)

comment:2 Changed 8 years ago by gilhad

Needs documentation: set

comment:3 Changed 8 years ago by Malcolm Tredinnick

Has patch: unset
Summary: Admin does correctly prefill DataTimeField from URL (GET) + patchAdmin does not correctly prefill DataTimeField from URL

Edited description for clarity.

Please attach a proper patch to the ticket, rather than mixing the proposed changes into the description of the problem. It makes things a lot easier for reviewing and further improvements (if subsequent patches are made).

Changed 8 years ago by gilhad

Attachment: AdminDateTimePatch.diff added

comment:4 Changed 8 years ago by gilhad

Has patch: set
Needs documentation: unset

Added patch for code AND documentation

comment:5 Changed 8 years ago by Jacob

Triage Stage: UnreviewedAccepted

comment:6 Changed 7 years ago by Chris Beaven

Needs tests: set

comment:7 Changed 6 years ago by Luke Plant

Severity: Normal
Type: Bug

comment:8 Changed 6 years ago by Alexander Herrmann

Easy pickings: unset
Owner: changed from nobody to Alexander Herrmann
Status: newassigned
UI/UX: unset

Changed 6 years ago by Alexander Herrmann

Attachment: 9739-r16345.diff added

comment:9 Changed 6 years ago by Alexander Herrmann

Needs tests: unset

attached a new diff for the current source and added test

Changed 5 years ago by Gandalfar

Attachment: ticket9737.diff added

patch against r17364

comment:10 Changed 5 years ago by Gandalfar

I've tested that patch works correctly with current trunk and cleaned-up docs syntax.

comment:11 Changed 3 years ago by Aymeric Augustin

I dislike the idea of blessing hand-crafted URLs as a public API.

comment:12 Changed 2 years ago by Tim Graham

Patch needs improvement: set

In any case, the patch needs to be updated to apply cleanly.

comment:13 in reply to:  12 Changed 21 months ago by Ridley Larsen

Replying to timo:

In any case, the patch needs to be updated to apply cleanly.

I have attached a patch that applies cleanly and passes testing.

comment:14 Changed 21 months ago by Ridley Larsen

Owner: changed from Alexander Herrmann to Ridley Larsen

comment:15 Changed 21 months ago by Claude Paroz

Ridley, would it be possible to turn the patch into a Github pull request?

comment:16 Changed 21 months ago by Tim Graham

Patch needs improvement: unset

comment:17 Changed 20 months ago by Markus Holtermann

It feels to me like using a single white space instead of a comma is more intuitive. 2015-04-05 12:34:56 vs 2015-04-05,12:34:56

comment:18 Changed 20 months ago by Tim Graham

Patch needs improvement: set

Changed 19 months ago by Ridley Larsen

Attachment: patch9739.diff added

patch against a2e56db

comment:19 Changed 19 months ago by Ridley Larsen

I've attached a new patch with a different approach. Instead of special-casing the SplitDateTimeWidget, I modified its' decompress method to account for string values. Thanks Tim for the advice on the isinstance call.

comment:20 Changed 19 months ago by Tim Graham

Patch needs improvement: unset

comment:21 Changed 19 months ago by Tim Graham

Patch needs improvement: set

comment:22 Changed 19 months ago by Tim Graham

#19431 is a duplicate with an alternate approach that might be worth looking at to see if any bits could be incorporated.

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