I was writing a unittest and noticed that I couldn't assign a datetime object to the models.DateTimeField without loosing the time details. I.e:
class Foo(models.Model):
created = models.DateTimeField(
editable=False)
my_time = datetime(2012, 01, 02, 1)
print "My Time", Foo.objects.create(created=my_time).created
Prints:
My time 2012-01-02 00:00:00
However if I do
print "My Time", Foo.objects.create(created='2012-01-02 01:00:00').created
I get:
My time 2012-01-02 01:00:00
What's the proper way of assigning a datetime to a models.DateTimeField?
You are assigning the datetime in the correct way.
If you can come up with a reproducible test case, then this is a bug, and you should file a ticket at http://code.djangoproject.com/
At a minimum, you'll need a complete models.py
(including things like import
statements), any settings that you need to change from a default settings.py
file, and a sequence of commands that you can run from a python shell that illustrate the issue.
The database that you are using will likely relavent -- is this a problem that affects only SQLite, or MySQL, or are you using a third-party database adapter?
If you can get that together, then definitely post a bug report -- the help you will find there will also go towards fixing the underlying issue for everyone else.