Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may be present

I get the bellow error in the console, when I run server in my PyCharm:

ERRORS:
frontend.Users.ctime: (fields.E160) The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may be present.
frontend.Users.uptime: (fields.E160) The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may be present.

My models Users code is below:

class Users(models.Model):
    ctime = models.DateTimeField(auto_now_add=True, default=datetime.now())
    uptime = models.DateTimeField(auto_now=True, default=datetime.now())   

Why I get this error?

like image 642
aircraft Avatar asked Jan 19 '26 17:01

aircraft


2 Answers

Just use:

class Users(models.Model):
    ctime = models.DateTimeField(auto_now_add=True)
    uptime = models.DateTimeField(auto_now=True)

It will work.

Explanation:

These both are mutually exclusive means you should use only one of them, not both.

like image 88
Piyush Maurya Avatar answered Jan 22 '26 07:01

Piyush Maurya


I got the same error below:

ERRORS: app1.Person.updated_at: (fields.E160) The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may be present.

Because I set both auto_now=True and auto_now_add=True to DateTimeField() as shown below:

# "app1/models.py"

class Person(models.Model):
    ...
    updated_at = models.DateTimeField(
        auto_now=True, # Here 
        auto_now_add=True # Here
    )
    ...

So, I removed one of them as shown below, then the error was solved:

# "app1/models.py"

class Person(models.Model):
    ...
    updated_at = models.DateTimeField(
        # auto_now=True, # Here 
        auto_now_add=True
    )
    ...
# "app1/models.py"

class Person(models.Model):
    ...
    updated_at = models.DateTimeField(
        auto_now=True,
        # auto_now_add=True # Here
    )
    ...

In addition, the doc says below in DateField.auto_now_add:

The options auto_now_add, auto_now, and default are mutually exclusive. Any combination of these options will result in an error.

like image 22
Kai - Kazuya Ito Avatar answered Jan 22 '26 06:01

Kai - Kazuya Ito



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!