[Django] #35906: SplitDateTime widget crashes while processing initials

8 views
Skip to first unread message

Django

unread,
Nov 12, 2024, 4:49:12 AM11/12/24
to django-...@googlegroups.com
#35906: SplitDateTime widget crashes while processing initials
-------------------------------------+-------------------------------------
Reporter: Михаил Акопян | Type: Bug
Status: new | Component: Forms
Version: 5.1 | Severity: Normal
Keywords: SplitDateTimeWidget | Triage Stage:
AdminSplitDateTime | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
I have a model with DateTime field. I want to set initials for this field
in django admin by using query params. For example:


{{{
# models.py

class MyModel(models.Model):
created_at = models.DateTimeField()


# admin.py

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
pass
}}}


Trying to create new object of MyModel with initial for created)at field:
`http://127.0.0.1:8000/admin/myapp/mymodel/add/?created_at=2024-10-10%2005%3A57%3A31`

Admin sites crashes with 500 error. This happens because the method
`decompress` of the `SplitDateTimeWidget` expects an object of datetime
type, but it is passed the string instead.

Proposed fix:



{{{
def decompress(self, value):
if isinstance(value, str):
try:
value = datetime.fromisoformat(value)
except ValueError:
return [None, None]
if value:
value = to_current_timezone(value)
return [value.date(), value.time()]
return [None, None]
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35906>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 12, 2024, 4:50:35 AM11/12/24
to django-...@googlegroups.com
#35906: SplitDateTime widget crashes while processing initials
-------------------------------------+-------------------------------------
Reporter: Михаил Акопян | Owner: (none)
Type: Bug | Status: new
Component: Forms | Version: 5.1
Severity: Normal | Resolution:
Keywords: SplitDateTimeWidget | Triage Stage:
AdminSplitDateTime | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Михаил Акопян:

Old description:
New description:

I have a model with DateTime field. I want to set initials for this field
in django admin by using query params. For example:


{{{
# models.py

class MyModel(models.Model):
created_at = models.DateTimeField()


# admin.py

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
pass
}}}


Trying to create new object of MyModel with initial for created_at field:
`http://127.0.0.1:8000/admin/myapp/mymodel/add/?created_at=2024-10-10%2005%3A57%3A31`

Admin sites crashes with 500 error. This happens because the method
`decompress` of the `SplitDateTimeWidget` expects an object of datetime
type, but it is passed the string instead.

Proposed fix:



{{{
def decompress(self, value):
if isinstance(value, str):
try:
value = datetime.fromisoformat(value)
except ValueError:
return [None, None]
if value:
value = to_current_timezone(value)
return [value.date(), value.time()]
return [None, None]
}}}

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35906#comment:1>

Django

unread,
Nov 12, 2024, 7:22:36 AM11/12/24
to django-...@googlegroups.com
#35906: SplitDateTime widget crashes while processing initials
-------------------------------------+-------------------------------------
Reporter: Михаил Акопян | Owner: (none)
Type: Bug | Status: closed
Component: Forms | Version: 5.1
Severity: Normal | Resolution: duplicate
Keywords: SplitDateTimeWidget | Triage Stage:
AdminSplitDateTime | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: => duplicate
* status: new => closed

Comment:

Duplicate of #9739
--
Ticket URL: <https://code.djangoproject.com/ticket/35906#comment:2>
Reply all
Reply to author
Forward
0 new messages