"Fixed default value provided" after upgrading to Django 1.8

17,001

Solution 1

The function datetime.now() is currently executed as soon as your code is imported, i.e. when you (re)start your server. All subsequent model instances will have the same value.

Instead, you should pass a callable function to default, that is executed each time a model instance needs a default value. The hint wants to convey that you should literally use DateField(default=django.utils.timezone.now) without the parentheses.

The message is slightly misleading, but Django doesn't know whether you used datetime.now() or django.utils.timezone.now().

Solution 2

The difference between timezone.now() and datetime.now() has been explained well in the above answers. However, the reason you're getting an error is because you are running the function which will set the default time as the time while applying migrations to the database.

All you had to do was use,

my_date = DateField(default=datetime.now)

instead of

my_date = DateField(default=datetime.now())

In the above method, the timezone.now function will be called while inserting/ modifying an Object.

Solution 3

Totally agree with @knbk

In my implementation I have made import first

my models.py:

from django.utils import timezone

and then, when was creating a model, I did this:

create_date = models.DateTimeField(default=timezone.now)

so the total code was:

from django.db import models
from django.utils import timezone
from django.urls import reverse

# Create your models here.

class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    text = models.TextField()
    create_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateTimeField(blank=True,null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def approve_comments(self):
        return self.comments.filter(approve_comments=True)

    def __str__(self):
        return self.title
Share:
17,001
Troy
Author by

Troy

Updated on June 03, 2022

Comments

  • Troy
    Troy about 2 years

    Django 1.8 now has some problem detection for models, which is nice. However, for one warning that it is giving me, I understand the problem, but I don't understand how the hint that it is giving me is any better.

    This is my (bad) model field:

    my_date = DateField(default=datetime.now())
    

    and it's easy to see why that's bad. But this is the hint it's giving me:

    MyMoel.my_date: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`

    So, it says to use timezone.now, but how is that any better than datetime.now? They're both "fixed default" values... timezone.now just returns a datetime instance, which is a fixed value...

    I suspect that it actually wants me to insert some sort of flag that says "use timezone.now later". But that's not what the hint says... so what is that flag?

  • Troy
    Troy about 9 years
    So giving it any callable will make the validator happy (i.e. I could pass it either datetime.now or timezone.now, or anything that is a function that returns a date)?
  • knbk
    knbk about 9 years
    Yes, any of them should make the validator (and ultimately, you) happy.