Code

Opened 6 years ago

Last modified 5 weeks ago

#9739 assigned Bug

Admin does not correctly prefill DataTimeField from URL

Reported by: gilhad Owned by: dArignac
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: no
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 (3)

AdminDateTimePatch.diff (1.2 KB) - added by gilhad 6 years ago.
9739-r16345.diff (3.4 KB) - added by dArignac 3 years ago.
ticket9737.diff (3.4 KB) - added by Gandalfar 3 years ago.
patch against r17364

Download all attachments as: .zip

Change History (14)

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 5 years ago by jacob

  • Triage Stage changed from Unreviewed to Accepted

comment:6 Changed 5 years ago by SmileyChris

  • Needs tests set

comment:7 Changed 3 years ago by lukeplant

  • Severity set to Normal
  • Type set to Bug

comment:8 Changed 3 years ago by dArignac

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

Changed 3 years ago by dArignac

comment:9 Changed 3 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 5 weeks ago by aaugustin

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as assigned
The owner will be changed from dArignac 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'
Author


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

 
Note: See TracTickets for help on using tickets.