[Django] #30238: Exception when saving model created with string for DateField

15 views
Skip to first unread message

Django

unread,
Mar 6, 2019, 10:20:09 PM3/6/19
to django-...@googlegroups.com
#30238: Exception when saving model created with string for DateField
-------------------------------------+-------------------------------------
Reporter: Mitchell | Owner: nobody
Harvey |
Type: Bug | Status: new
Component: Database | Version: 2.1
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
If you instantiate a new model object with a keyword argument value for a
DateField given a string, an exception is thrown when the object is saved.
However, the model is saved anyways.

Example Error:
AttributeError: 'str' object has no attribute 'isoformat'

Call Stack:
{{{
File "REDACTED", line 746, in migrate_remote_unactive_driver
local_driver.save()
File "REDACTED", line 171, in save
super(Driver, self).save(*args, **kwargs)
File "REDACTED/env/lib/python3.6/site-
packages/django/db/models/base.py", line 729, in save
force_update=force_update, update_fields=update_fields)
File "REDACTED/env/lib/python3.6/site-
packages/django/db/models/base.py", line 769, in save_base
update_fields=update_fields, raw=raw, using=using,
File "REDACTED/env/lib/python3.6/site-
packages/django/dispatch/dispatcher.py", line 178, in send
for receiver in self._live_receivers(sender)
File "REDACTED/env/lib/python3.6/site-
packages/django/dispatch/dispatcher.py", line 178, in <listcomp>
for receiver in self._live_receivers(sender)
File "REDACTED", line 12, in create_launch_list
serialized_obj = serializers.serialize('json', [instance])
File "REDACTED/env/lib/python3.6/site-
packages/django/core/serializers/__init__.py", line 128, in serialize
s.serialize(queryset, **options)
File "REDACTED/env/lib/python3.6/site-
packages/django/core/serializers/base.py", line 89, in serialize
self.handle_field(obj, field)
File "REDACTED/env/lib/python3.6/site-
packages/django/core/serializers/python.py", line 51, in handle_field
self._current[field.name] = self._value_from_field(obj, field)
File "REDACTED/env/lib/python3.6/site-
packages/django/core/serializers/python.py", line 47, in _value_from_field
return value if is_protected_type(value) else
field.value_to_string(obj)
File "REDACTED/env/lib/python3.6/site-
packages/django/db/models/fields/__init__.py", line 1281, in
value_to_string
return '' if val is None else val.isoformat()
AttributeError: 'str' object has no attribute 'isoformat'

}}}

django/db/models/fields/__init__.py

value_to_string(self, obj)

Presumes that obj is not of type string already.

To Reproduce:

Create a model with a a field of type 'DateField'

{{{
class MyModel(models.Model):

worthless_field = models.DateField(null=True, blank=True)
}}}

Instantiate and save an object of type 'MyModel' in a view.py:

{{{
my_model_args = { 'worthless_field' : '2019-02-07'}
myExceptionalModel = MyModel( **my_model_args)

# Exception thrown here
myExceptionalModel.save()

# Although, model is actually saved.
}}}


Please check if the value is a valid datetime string already before
attempting to convert it to a string. It would be nice if a model was not
saved when an exception is thrown.

Or do whatever you like, I have to work around it anyways.

Good luck, love Django, great job guys!

--
Ticket URL: <https://code.djangoproject.com/ticket/30238>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 7, 2019, 3:50:04 AM3/7/19
to django-...@googlegroups.com
#30238: Exception when saving model created with string for DateField
-------------------------------------+-------------------------------------
Reporter: Mitchell Harvey | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* Attachment "ticket_30238.zip" added.

Sample app with provided model and test case.

Django

unread,
Mar 7, 2019, 3:50:40 AM3/7/19
to django-...@googlegroups.com
#30238: Exception when saving model created with string for DateField
-------------------------------------+-------------------------------------
Reporter: Mitchell Harvey | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

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


Comment:

The `AttributeError` is not raised. This test fails as the
`assertRaisesMessage()` call:

{{{
class Tests(TestCase):

def test_attribute_error_stops_save(self):

my_model_args = { 'worthless_field' : '2019-02-07'}
myExceptionalModel = MyModel( **my_model_args)

msg = "'str' object has no attribute 'isoformat'"
with self.assertRaisesMessage(AttributeError, msg):
myExceptionalModel.save()

self.assertEqual(0, MyModel.objects.count())
}}}

Behaviour is as expected:


{{{
>>> myExceptionalModel.save()
>>> myExceptionalModel.refresh_from_db()
>>> myExceptionalModel.worthless_field
datetime.date(2019, 2, 7)
}}}

Sample app with model and test case attached.

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

Django

unread,
Mar 7, 2019, 8:19:06 AM3/7/19
to django-...@googlegroups.com
#30238: Exception when saving model created with string for DateField
-------------------------------------+-------------------------------------
Reporter: Mitchell Harvey | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

Looking at the traceback in the ticket description, the crash comes from a
signal handler, `create_launch_list`.

--
Ticket URL: <https://code.djangoproject.com/ticket/30238#comment:2>

Reply all
Reply to author
Forward
0 new messages