Timestamp fields in django

前端 未结 4 1238
谎友^
谎友^ 2020-12-16 12:57

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

4条回答
  •  借酒劲吻你
    2020-12-16 13:06

    The pip package django-unixdatetimefield provides a UnixDateTimeField field that you can use for this out of the box (https://pypi.python.org/pypi/django-unixdatetimefield/).

    Example model:

    from django_unixdatetimefield import UnixDateTimeField
    
    class MyModel(models.Model):
        created_at = UnixDateTimeField()
    

    Python ORM query:

    >>> m = MyModel()
    >>> m.created_at = datetime.datetime(2015, 2, 21, 19, 38, 32, 209148)
    >>> m.save()
    

    Database:

    sqlite> select created_at from mymodel;
    1426967129
    

    Here's the source code if interested - https://github.com/Niklas9/django-unixdatetimefield.

    Disclaimer: I'm the author of this pip package.

提交回复
热议问题