Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,28 @@ async def test_update_auto_now(db):
assert obj1.updated_at.date() == updated_at.date()


@pytest.mark.asyncio
async def test_update_auto_now_with_update_fields(db):
tournament = await Tournament.create(name="1")
event = await Event.create(name="original", tournament=tournament)
original_modified = event.modified

# Set modified to the past so we can detect if it gets updated
past = timezone.now() - timedelta(days=1)
await Event.filter(pk=event.pk).update(modified=past)

event = await Event.get(pk=event.pk)
assert event.modified.date() == past.date()

# Update only name with update_fields; auto_now field should also be updated
event.name = "updated"
await event.save(update_fields=["name"])

event = await Event.get(pk=event.pk)
assert event.name == "updated"
assert event.modified.date() == timezone.now().date()


@pytest.mark.asyncio
async def test_update_relation(db):
tournament_first = await Tournament.create(name="1")
Expand Down
6 changes: 6 additions & 0 deletions tortoise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,12 @@ async def save(
raise IncompleteInstanceError(
f"{self.__class__.__name__} is a partial model, can only be saved with the relevant update_field provided"
)
if update_fields:
update_fields = list(update_fields)
for field_name, field_obj in self._meta.fields_map.items():
if field_name not in update_fields and getattr(field_obj, "auto_now", False):
update_fields.append(field_name)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about queryset .update() and .bulk_update() methods?


await self._pre_save(db, update_fields)

if force_create:
Expand Down