model-bakers / model_bakery

Object factory for Django
https://model-bakery.readthedocs.io/en/latest/
Other
845 stars 85 forks source link

auto_now_add=True Fields do not match baker.make return #469

Open nweave03 opened 11 months ago

nweave03 commented 11 months ago

Describe the issue A clear and concise description of what the issue is.

I am using Django 5.0, and found what I think is a bug. This does not occur if i fall back to model_bakery 1.16.0, it was introduced in 1.17.0. I searched but I did not see anything on it. Apologies if i just missed it, or if this is expected behavior and I didn't see the changelog on it.

When using a Djano DateTimeField with auto_now_add=True like so:


   created = models.DateTimeField(
        auto_now_add=True, db_index=True, verbose_name=_("created")
    )

The baker.make return does not match what is in the database.

baker.make return: {'_state': <django.db.models.base.ModelState object at 0x1158fb650>, 'created': datetime.datetime(2023, 12, 28, 18, 49, 21, 667003, tzinfo=datetime.timezone.utc), }

objects.all()[0].dict return: {'_state': <django.db.models.base.ModelState object at 0x1157bf990>, 'created': datetime.datetime(2023, 12, 28, 18, 49, 21, 666729, tzinfo=datetime.timezone.utc), }

To Reproduce

Create any model with an auto_now_add time field. Then compare the output of the baker.make function with what is in the database. They do not match.

Expected behavior I expect that the baker.make function returns an instance of the database object. I use this for comparisons with tests. When the datetime fields do not match, I have to look for the error.

Versions