#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:
> 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]
> }}}
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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/010701931fc81f42-401e0598-2646-41b0-903b-89bfcf0711b6-000000%40eu-central-1.amazonses.com.