#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
   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
   File "REDACTED/env/lib/python3.6/site-
 packages/django/db/models/fields/__init__.py", line 1281, in
     return '' if val is None else val.isoformat()
 AttributeError: 'str' object has no attribute 'isoformat'



 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

 # 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.

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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
For more options, visit https://groups.google.com/d/optout.

Reply via email to