Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7769715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:13:35+00:00 2026-06-01T16:13:35+00:00

I was writing a unittest and noticed that I couldn’t assign a datetime object

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:13:36+00:00Added an answer on June 1, 2026 at 4:13 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a unittest for a module that accepts command line arguments. I've
I'm writing a unit test for a Text Scrubber utility that will remove any
I am writing a unit test that needs to access an image file that
I am having some trouble with writing a unit test that checks my custom
I am writing a c# unit test that validates string properties for an ORM
I'm writing tests for a Django application that uses an external data source .
We are writing unit tests for our ASP.NET application that run against a test
I'm writing a testing framework using Python's unittest. I have separate modules for separate
I am writing some code that uses poplib and imaplib to collect emails through
I am currently writing unit tests for a ViewModel in my project that uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.