Skip to content

Commit

Permalink
Merge pull request #62 from hello-wn/feat-save
Browse files Browse the repository at this point in the history
使用only参数更新记录时,总是更新updated_at字段
  • Loading branch information
fenngwd authored Dec 29, 2021
2 parents d57f2e3 + 5fe0f9c commit 8956201
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

## [1.2.6] - 2021-12-29

- Update `updated_at` field implicitly when updating with the `only` option.
```
some_record.a_field = "updatede a field"
# updated fields: a_field, updated_at
some_record.save(only=["a_field"])
```


## [1.2.3] - 2021-04-21

- ensure_ascii is provided in JSONCharField to support storing Chinese in non-ascii way
Expand Down
2 changes: 1 addition & 1 deletion peeweext/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.2.5'
__version__ = '1.2.6'
3 changes: 3 additions & 0 deletions peeweext/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def save(self, *args, **kwargs):

pk_value = self._pk
created = kwargs.get('force_insert', False) or not bool(pk_value)
if not created and kwargs.get("only"):
# update `updated_at` field implicitly when using the `only` option
kwargs["only"].append("updated_at")
pre_save.send(type(self), instance=self, created=created)
ret = super().save(*args, **kwargs)
post_save.send(type(self), instance=self, created=created)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,18 @@ def test_validator(table):
note.save()
# equivalent expressions for skip validate "message"
note.save(skip_validation=True)

old_updated_at = Note.get_by_id(note.id).updated_at
note.save(only=[Note.published_at])
note.save(only=["published_at"])
assert Note.get_by_id(note.id).updated_at > old_updated_at

old_updated_at = Note.get_by_id(note.id).updated_at
note.message = "try to update"
note.save(only=[Note.message, Note.updated_at])
latest_note = Note.get_by_id(note.id)
assert latest_note.message == note.message
assert latest_note.updated_at > old_updated_at

note.message = 'message'
note.save()
Expand Down

0 comments on commit 8956201

Please sign in to comment.