Datetime Field Received Naive Datetime

I came across a classic DateTimeField received a naive datetime while time zone support is activetwist warning . The error occurs when I run tests that use the factories provided factory_boy. The following is an example factory:

from django.utils.timezone import now
import factory
class PostFactory(factory.DjangoModelFactory):
    FACTORY_FOR = models.Post
    value = 42
    created = now()

As you can see, I am using the now () method from the Django timezone, which should take care of all the naive datetime thing, but it is not. Here's what the model looks like:

class Post(models.Model)
    value = models.IntegerField()
    created = models.DateTimeField(auto_now_add=True)

Also in my settings.py file I installed USE_TZ = True.

I tried installing pytzand using its libraries to create a datetime object to populate a field in a factory, but this also does not work.

I know that I can suppress the warning, but it is already starting to bite me in other areas of the code, and I would like to deal with it.,

+5
2

timezone settings.py.

USE_TZ False, , Django . USE_TZ - True, , Django , .

0

:

import factory
from django.utils import timezone


class PostFactory(factory.DjangoModelFactory):
    FACTORY_FOR = models.Post
    value = 42
    created = factory.Faker("date_time", tzinfo=timezone.get_current_timezone())
0

All Articles