khelll
khelll

Reputation: 24010

Setting a model attribute to some sql value in Django

I have a model called job and I want to set a datetime attribute (started_time) to MySQL now() value. how can I do that in Django?

I don't want to use the model auto_now or auto_now_add methods, since I have other applications who share the same DB and I don't want to handle timezones, thus I want to delegate that to MySQL

Upvotes: 0

Views: 150

Answers (2)

Tomasz Zieliński
Tomasz Zieliński

Reputation: 16367

Don't use auto_now/auto_add_now, they are problematic. Instead, do this:

  started_time = models.DateTimeField(default=datetime.utcnow)

-- assuming that you're working with timestamps in UTC.

Upvotes: 2

Dmitry Shevchenko
Dmitry Shevchenko

Reputation: 32424

Use MySQL trigger.

Upvotes: 0

Related Questions