简体   繁体   中英

How to auto_now_add to django DateTimeField only if field was not used as objects.create() parameter?

I have DateTimeField:

class MyModel(models.Model):
    ...
    created_at = models.DateTimeField(auto_now_add=True)

I want to use auto_now_add option only if entry was created with admin or with constructor like this:

instance = MyModel.objects.create(no_created_at_there)

Otherwise I want to use value used in constructor. How to realise that?

Problem was resolved by Shang Wang's suggestion from question's comment. I've just replaced auto_now_add parameter with default=datetime.now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM