Opened 14 years ago
Closed 14 years ago
#14037 closed (duplicate)
models.DateField don't serialize JSON
Reported by: | Carlo Pires | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.2 |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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:
from django.db import models class BugSerial(models.Model): date = models.DateField(auto_now=True)
Now, after syncdb, the bug:
./manage.py shell
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.']
Change History (3)
comment:1 by , 14 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|
comment:2 by , 14 years ago
I think this is a bug. models.DateField with auto_now should use datetime.date.today() as init value.
comment:3 by , 14 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
I think this is a duplicate of #10970 (and #10602 that IMHO was incorrectly closed as invalid.)