Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RuntimeWarning: DateTimeField received a naive datetime for auto_now

We're using Django 1.10

We're getting a lot of this warning:

RuntimeWarning: DateTimeField Item.updated_at received a naive datetime (2018-05-01 12:35:18.213471) while time zone support is active.
RuntimeWarning)

I read a lot of answers about that questions, but in that case we're not settings the date manually. That field (Item.updated_at) is set as

auto_now=True

Is there a way to make 'auto_now' not naive?

This is part of the model:

class BaseModel(models.Model):

    id = models.UUIDField(default=uuid.uuid4, editable=False, db_index=True, unique=True, primary_key=True)
    created_by = models.CharField(max_length=200)
    created_at = models.DateTimeField(db_index=True, auto_now_add=True)
    updated_by = models.CharField(max_length=200)
    updated_at = models.DateTimeField(db_index=True, auto_now=True)

Thanks

EDIT: Could it be related to the factories we're using in tests? For example:

class ItemFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Item

    title = "Fake item title"
    identifier = factory.Sequence(lambda n: n)
    status_id = Status.Open['id']
    due_date = None
    updated_by = "Fake updater"
    updated_at = timezone.now()
like image 750
user2880391 Avatar asked May 01 '18 13:05

user2880391


1 Answers

I think the issue indeed might be in the settings - check if you have USE_TZ enabled in your setting file. Ref: https://docs.djangoproject.com/en/3.2/ref/settings/#std:setting-USE_TZ

like image 157
meester palaba Avatar answered Sep 28 '22 04:09

meester palaba