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 8179135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:56:09+00:00 2026-06-06T23:56:09+00:00

I have a MySQL database, right now I’m generating all of the datetime fields

  • 0

I have a MySQL database, right now I’m generating all of the datetime fields as models.DateTimeField. Is there a way to get a timestamp instead? I want to be able to autoupdate on create and update etc.

The documentation on django doesn’t have this?

  • 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-06T23:56:10+00:00Added an answer on June 6, 2026 at 11:56 pm

    There was actually a very good and informative article on this. Here:
    http://ianrolfe.livejournal.com/36017.html

    The solution on the page is slightly deprecated, so I did the following:

    from django.db import models
    from datetime import datetime
    from time import strftime
    
    class UnixTimestampField(models.DateTimeField):
        """UnixTimestampField: creates a DateTimeField that is represented on the
        database as a TIMESTAMP field rather than the usual DATETIME field.
        """
        def __init__(self, null=False, blank=False, **kwargs):
            super(UnixTimestampField, self).__init__(**kwargs)
            # default for TIMESTAMP is NOT NULL unlike most fields, so we have to
            # cheat a little:
            self.blank, self.isnull = blank, null
            self.null = True # To prevent the framework from shoving in "not null".
    
        def db_type(self, connection):
            typ=['TIMESTAMP']
            # See above!
            if self.isnull:
                typ += ['NULL']
            if self.auto_created:
                typ += ['default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP']
            return ' '.join(typ)
    
        def to_python(self, value):
            if isinstance(value, int):
                return datetime.fromtimestamp(value)
            else:
                return models.DateTimeField.to_python(self, value)
    
        def get_db_prep_value(self, value, connection, prepared=False):
            if value==None:
                return None
            # Use '%Y%m%d%H%M%S' for MySQL < 4.1
            return strftime('%Y-%m-%d %H:%M:%S',value.timetuple())
    

    To use it, all you have to do is:
    timestamp = UnixTimestampField(auto_created=True)

    In MySQL, the column should appear as:
    'timestamp' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

    Only drawback with this is that it only works on MySQL databases. But you can easily modify it for others.

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

Sidebar

Related Questions

Right now I have a mysql database with a table userphone(id, phone, type,userid) and
I'm new to mysql. Right now, I have this kind of structure in mysql
Right now in a database I have a Members table and a Products table
Right now I have a C++ client application that uses mysql.h to connect to
I am using Spring 3 and grabbing users from a MySQL database. Right now,
Right now I am working with a text file that happens to have all
I have mysql database like this id | code 1 | bok-1 2 |
I have mysql database and I want a software which can draw the database
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
Here my code, I need to insert into mysql database I have mysql,php,android 1)

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.