Thursday, May 29, 2014

Date string do not parsed to date object in Model.objects.create

I have a model like:

    class SomeModel(Model):
        created = DateField(...)

I created an object using a date string (instead of date object)

    > new_rec = SomeModel.objects.create(created="2014-05-28")
    > new_rec.created
    >> "2014-05-28"
    > type(new_rec.created)
    >> str

While django save "created" field as date to my database, new_rec object keep it as a string since. When I retreive this record from database, it displayed as a normal date object.

    > rec = SomeModel.objects.get(id=...)
    > rec.created
    >> datetime.date(2014, 5, 28)

Problem is, django do not parse the the field value to related field type defied in Model and update the object on create operations.
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ee80c4ca-5f78-409d-98dc-e14fe8d7667c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment