tortoise / tortoise/tortoise-orm
Ensure that auto_now=True fields gets updated on a update-query & add model.update_values(**) — Was: Handling modified_at (auto_now=True) with pydantic models
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
Hello!
How to update, for example (modified_at) field, using update model method. Example:
class User(Model):
id = fields.IntField(pk=True)
username = fields.CharField(max_length=50)
created_at = fields.DatetimeField(auto_now_add=True)
modified_at = fields.DatetimeField(auto_now=True)
UserUpdate_Pydantic = pydantic_model_creator(User, name="UserUpdate", exclude_readonly=True)```
user = UserUpdate_Pydantic(username='user_modified')
User.filter(id=user_id).update(**user.dict(exclude_unset=True))
That way, the modified_at field don't update because it is not pass to update model method
My question is: How can I update modified_at field implicitly
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the User.filter(...).update(...) path and the model update behavior shown in the example, then trace how auto_now=True fields are handled during query updates. Review the proposed update_values(**) API against the Pydantic update flow; done means the intended behavior is agreed and verified for both update paths.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- database
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100