Opened 10 years ago
Last modified 2 years 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)
Change History (27)
comment:1 Changed 10 years ago by
comment:2 Changed 10 years ago by
Needs documentation: | set |
---|
comment:3 Changed 10 years ago by
Has patch: | unset |
---|---|
Summary: | Admin does correctly prefill DataTimeField from URL (GET) + patch → 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 10 years ago by
Attachment: | AdminDateTimePatch.diff added |
---|
comment:4 Changed 10 years ago by
Has patch: | set |
---|---|
Needs documentation: | unset |
Added patch for code AND documentation
comment:5 Changed 10 years ago by
Triage Stage: | Unreviewed → Accepted |
---|
comment:6 Changed 9 years ago by
Needs tests: | set |
---|
comment:7 Changed 8 years ago by
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:8 Changed 8 years ago by
Easy pickings: | unset |
---|---|
Owner: | changed from nobody to Alexander Herrmann |
Status: | new → assigned |
UI/UX: | unset |
Changed 8 years ago by
Attachment: | 9739-r16345.diff added |
---|
comment:9 Changed 8 years ago by
Needs tests: | unset |
---|
attached a new diff for the current source and added test
comment:10 Changed 7 years ago by
I've tested that patch works correctly with current trunk and cleaned-up docs syntax.
comment:12 follow-up: 13 Changed 5 years ago by
Patch needs improvement: | set |
---|
In any case, the patch needs to be updated to apply cleanly.
comment:13 Changed 4 years ago by
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 4 years ago by
Owner: | changed from Alexander Herrmann to Ridley Larsen |
---|
comment:15 Changed 4 years ago by
Ridley, would it be possible to turn the patch into a Github pull request?
comment:17 Changed 4 years ago by
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 4 years ago by
Patch needs improvement: | set |
---|
comment:19 Changed 4 years ago by
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 4 years ago by
Patch needs improvement: | unset |
---|
comment:21 Changed 4 years ago by
Patch needs improvement: | set |
---|
comment:22 Changed 4 years ago by
#19431 is a duplicate with an alternate approach that might be worth looking at to see if any bits could be incorporated.
comment:23 Changed 2 years ago by
I updated to Djago 1.10 on more sites, the problem still remains (cannot prefill admins datetime field) and the original fix still works (just the lines moved after line 1404 now).
Would be possible to somehow resolve it and incorporate it in main stream, so I would not be forced to manually patch it each and every version? Please, prettty, pretty please ...
Typo - (Admin does NOT correctly prefill DataTimeField from URL (GET) + patch)