0

How can I set default value for timestp field ?

Default value > 'NOW()'

class Timestamp(models.Field):
    def db_type(self, connection):
        return 'timestamp'


class MyModel(Model):
    # ...
    my_field = Timestamp()
1

1 Answer 1

1

You don't need to write a class for this. Django has auto_now_add attribute.

class MyModel(Model):
    # ...
    my_field = models.DateTimeField(auto_now_add=True, blank=True)

    #To save without timezone override the save method
    def save(self, *args, **kwargs):
        self.my_field = self.my_field.replace(tzinfo=None)
        super(MyModel, self).save(*args, **kwargs)
Sign up to request clarification or add additional context in comments.

2 Comments

I want datetime without zone. DateTimeField create field with timestampz.
its not a good idea in large applications with fifty models.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.