#14037: models.DateField don't serialize JSON
---------------------------+------------------------------------------------
 Reporter:  carlopires     |       Owner:  nobody    
   Status:  new            |   Milestone:            
Component:  Uncategorized  |     Version:  1.2       
 Keywords:                 |       Stage:  Unreviewed
Has_patch:  0              |  
---------------------------+------------------------------------------------
 When serializing records with DateField(auto_now=True), the serialized
 string is incorrect because created record is initialized with
 date.datetime
 and django.core.serializers.Serializer.handle_field don't call
 DateField.value_to_string() as date.datetime is listed in
 django.utils.encoding.is_protected_type.

 Start an app called bugmodel and create:

 {{{
 #!python
 from django.db import models

 class BugSerial(models.Model):
         date = models.DateField(auto_now=True)

 }}}

 Now, after syncdb, the bug:
 {{{
 ./manage.py shell
 }}}

 {{{
 #!python

 In [1]: from bugmodel.models import BugSerial

 In [2]: r = BugSerial()

 In [3]: r.save()

 In [4]: from django.core.serializers import serialize

 In [5]: serialized = serialize('json', [r])

 In [6]: serialized
 Out[6]: '[{"pk": 1, "model": "bugmodel.bugserial", "fields": {"date":
 "2010-07-30 14:24:09"}}]'

 In [7]: from django.core.serializers import deserialize

 In [8]: unserialized = deserialize('json', serialized)

 In [9]: unserialized
 Out[9]: <generator object Deserializer at 0x187d0f0>

 In [10]: list(unserialized)
 ---------------------------------------------------------------------------
 ValidationError                           Traceback (most recent call
 last)

 /home/carlo/bug/bug/<ipython console> in <module>()

 /usr/lib/python2.6/dist-packages/django/core/serializers/json.py in
 Deserializer(stream_or_string, **options)
      36     else:
      37         stream = stream_or_string
 ---> 38     for obj in PythonDeserializer(simplejson.load(stream),
 **options):
      39         yield obj
      40

 /usr/lib/python2.6/dist-packages/django/core/serializers/python.py in
 Deserializer(object_list, **options)
     126             # Handle all other fields

     127             else:
 --> 128                 data[field.name] = field.to_python(field_value)
     129
     130         yield base.DeserializedObject(Model(**data), m2m_data)

 /usr/lib/python2.6/dist-packages/django/db/models/fields/__init__.py in
 to_python(self, value)
     606
     607         if not ansi_date_re.search(value):
 --> 608             raise
 exceptions.ValidationError(self.error_messages['invalid'])
     609         # Now that we have the date string in YYYY-MM-DD format,
 check to make

     610         # sure it's a valid date.


 ValidationError: [u'Enter a valid date in YYYY-MM-DD format.']

 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/14037>
Django <http://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 post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to