[Answer]-Django equivalent of SqlAlchemy's literal_column

1👍

If you don’t care about having a central time authority:

import time

version = models.BigIntegerField(
    default = lambda: int(time.time()*1000000) )

To bend the database to your will:

from django.db.models.expressions import ExpressionNode

class NowInt(ExpressionNode):
    """ Pass this in the same manner you would pass Count or F objects """

    def __init__(self):
        super(Now, self).__init__(None, None, False)

    def evaluate(self, evaluator, qn, connection):
        return '(UNIX_TIMESTAMP() * 1000000 + MICROSECOND(CURRENT_TIMESTAMP))', []

### Model

version = models.BigIntegerField(default=NowInt())

because expression nodes are not callables, the expression will be evaluated database side.

👤Thomas

Leave a comment