Opened 6 years ago

Last modified 5 days ago

#9739 assigned Bug

Admin does not correctly prefill DataTimeField from URL

Reported by: gilhad Owned by: RidleyLarsen
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 6 years ago.
9739-r16345.diff (3.4 KB) - added by dArignac 4 years ago.
ticket9737.diff (3.4 KB) - added by Gandalfar 3 years ago.
patch against r17364
patch9739.diff (3.1 KB) - added by RidleyLarsen 3 weeks ago.
patch against a2e56db

Download all attachments as: .zip

Change History (22)

comment:1 Changed 6 years ago by gilhad

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

comment:2 Changed 6 years ago by gilhad

  • Needs documentation set

comment:3 Changed 6 years ago by mtredinnick

  • Has patch unset
  • Summary changed from Admin does correctly prefill DataTimeField from URL (GET) + patch to Admin 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 6 years ago by gilhad

comment:4 Changed 6 years ago by gilhad

  • Has patch set
  • Needs documentation unset

Added patch for code AND documentation

comment:5 Changed 6 years ago by jacob

  • Triage Stage changed from Unreviewed to Accepted

comment:6 Changed 6 years ago by SmileyChris

  • Needs tests set

comment:7 Changed 4 years ago by lukeplant

  • Severity set to Normal
  • Type set to Bug

comment:8 Changed 4 years ago by dArignac

  • Easy pickings unset
  • Owner changed from nobody to dArignac
  • Status changed from new to assigned
  • UI/UX unset

Changed 4 years ago by dArignac

comment:9 Changed 4 years ago by dArignac

  • Needs tests unset

attached a new diff for the current source and added test

Changed 3 years ago by Gandalfar

patch against r17364

comment:10 Changed 3 years ago by Gandalfar

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

comment:11 Changed 11 months ago by aaugustin

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

comment:12 follow-up: Changed 9 months ago by timo

  • Patch needs improvement set

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

Changed 3 weeks ago by RidleyLarsen

patch against a2e56db

comment:13 in reply to: ↑ 12 Changed 3 weeks ago by RidleyLarsen

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 3 weeks ago by RidleyLarsen

  • Owner changed from dArignac to RidleyLarsen

comment:15 Changed 3 weeks ago by claudep

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

comment:16 Changed 3 weeks ago by timgraham

  • Patch needs improvement unset

comment:17 Changed 2 weeks ago by MarkusH

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 5 days ago by timgraham

  • Patch needs improvement set
Note: See TracTickets for help on using tickets.
Back to Top