Skip to content

Commit 3353a8c

Browse files
committed
fix: should not skip auto now fields but assign with specified value
1 parent 8051e3f commit 3353a8c

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Added
1111

1212
### Changed
13+
- Fix regression introduced in 1.20.3 that prevented using `auto_now` and `auto_now_add` fields with seq or callable.
1314

1415
### Removed
1516

model_bakery/baker.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,6 @@ def _skip_field(self, field: Field) -> bool: # noqa: C901
590590
else:
591591
field.fill_optional = field.name in self.fill_in_optional
592592

593-
if _is_auto_datetime_field(field):
594-
return True
595-
596593
if isinstance(field, FileField) and not self.create_files:
597594
return True
598595

@@ -647,8 +644,13 @@ def _handle_auto_now(self, instance: Model, attrs: Dict[str, Any]):
647644
if not attrs:
648645
return
649646

647+
# use .update() to force update auto_now fields
650648
instance.__class__.objects.filter(pk=instance.pk).update(**attrs)
651649

650+
# to make the resulting instance has the specified values
651+
for k, v in attrs.items():
652+
setattr(instance, k, v)
653+
652654
def _handle_one_to_many(self, instance: Model, attrs: Dict[str, Any]):
653655
for key, values in attrs.items():
654656
manager = getattr(instance, key)

tests/test_baker.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,8 +1157,12 @@ def test_make_with_auto_now(self, use_tz, settings):
11571157
sent_date=now,
11581158
)
11591159

1160-
instance.refresh_from_db()
1160+
assert instance.created == now
1161+
assert instance.updated == now
1162+
assert instance.sent_date == now
11611163

1164+
# Should not update after refreshing from the db
1165+
instance.refresh_from_db()
11621166
assert instance.created == now
11631167
assert instance.updated == now
11641168
assert instance.sent_date == now
@@ -1169,9 +1173,14 @@ def test_make_with_auto_now_and_fill_optional(self):
11691173
models.ModelWithAutoNowFields,
11701174
_fill_optional=True,
11711175
)
1172-
created, updated = instance.created, instance.updated
1176+
created, updated, sent_date = (
1177+
instance.created,
1178+
instance.updated,
1179+
instance.sent_date,
1180+
)
11731181

1182+
# Should not update after refreshing from the db
11741183
instance.refresh_from_db()
1175-
11761184
assert instance.created == created
11771185
assert instance.updated == updated
1186+
assert instance.sent_date == sent_date

tests/test_recipes.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
DummyBlankFieldsModel,
2121
DummyNumbersModel,
2222
LonelyPerson,
23+
ModelWithAutoNowFields,
2324
Person,
2425
Profile,
2526
User,
@@ -669,3 +670,37 @@ def test_only_iterators_not_iteratables_are_iterated(self):
669670
DummyBlankFieldsModel, blank_text_field="not an iterator, so don't iterate!"
670671
)
671672
assert r.make().blank_text_field == "not an iterator, so don't iterate!"
673+
674+
675+
class TestAutoNowFields:
676+
@pytest.mark.django_db
677+
def test_make_with_auto_now_using_datetime_generator(self):
678+
delta = timedelta(minutes=1)
679+
680+
def gen():
681+
idx = 0
682+
while True:
683+
idx += 1
684+
yield TEST_TIME + idx * delta
685+
686+
r = Recipe(
687+
ModelWithAutoNowFields,
688+
created=gen(),
689+
)
690+
691+
assert r.make().created == tz_aware(TEST_TIME + 1 * delta)
692+
assert r.make().created == tz_aware(TEST_TIME + 2 * delta)
693+
694+
@pytest.mark.django_db
695+
def test_make_with_auto_now_using_datetime_seq(self):
696+
delta = timedelta(minutes=1)
697+
r = Recipe(
698+
ModelWithAutoNowFields,
699+
created=seq(
700+
TEST_TIME,
701+
increment_by=delta,
702+
),
703+
)
704+
705+
assert r.make().created == tz_aware(TEST_TIME + 1 * delta)
706+
assert r.make().created == tz_aware(TEST_TIME + 2 * delta)

0 commit comments

Comments
 (0)