[Solved]-Django model_to_dict skips all DateTimeField when converting models

18👍

I found the cause to be auto_now_add=True, it automatically sets editable=False, which causes model_to_dict() to skip the fields

0👍

I can not replicate your issue

>>> pprint(model_to_dict(User.objects.get(pk=1)))
{'date_joined': datetime.datetime(2013, 1, 21, 10, 56, 6, tzinfo=<UTC>),
 'email': u'g@o.org',
 'first_name': u'\u042d\u0434\u0443\u0430\u0440\u0434',
 'groups': [],
 'id': 1,
 'is_active': True,
 'is_staff': True,
 'is_superuser': True,
 'last_login': datetime.datetime(2013, 3, 26, 12, 10, 28, 834151, tzinfo=<UTC>),
 'last_name': u'\u0418\u0441\u043a\u0430\u043d\u0434\u0430\u0440\u043e\u0432',
 'password': u'pbkdf2_sha256$10000$n8t2YQB1MaZE$lVg5bbp22ixDNQCj7AjtzGJ3WzOUnJo24137fTOxwi4=',
 'user_permissions': [],
 'username': u'1'}

Leave a comment