Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: async default value #1498

Merged
merged 3 commits into from
Apr 27, 2024

Conversation

YAGregor
Copy link
Contributor

@YAGregor YAGregor commented Oct 15, 2023

allow Field's default value not only be callable, literal value, but also async function

Description

when set Field's default keyword option to async function, then while save and create model, if the field never set, the field will be set as the async function's result

Motivation and Context

I write a model and I want a serial of unique, auto increasing serial names generate from redis (by aioredis), but the field's default do not support retrieve value from async function, I have to override create method, that's cumbersome.

and tortoise is an async orm, it should support async first.

How Has This Been Tested?

I create a new model for new test case, test model's behavior when combine async default with save, create, and when default field value was covered by set attribute.

I run tests on linux, include sqlite's test cases.

Almost no effect.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@long2ice
Copy link
Member

long2ice commented Nov 3, 2023

Thanks! LGTM. Please update changelog also.

@coveralls
Copy link

coveralls commented Nov 3, 2023

Pull Request Test Coverage Report for Build 8858936385

Details

  • 17 of 17 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 87.923%

Totals Coverage Status
Change from base Build 8858003658: 0.02%
Covered Lines: 5758
Relevant Lines: 6453

💛 - Coveralls

YAGregor added a commit to YAGregor/tortoise-orm that referenced this pull request Nov 3, 2023
@YAGregor
Copy link
Contributor Author

YAGregor commented Nov 3, 2023

I've done the change log, do you think we should update the doc? Seems that this behavior is natural, and there is no need to @long2ice

YAGregor added a commit to YAGregor/tortoise-orm that referenced this pull request Nov 6, 2023
@abondar
Copy link
Member

abondar commented Apr 27, 2024

@YAGregor hi!

Please rebase PR on actual develop and resolve conflict in Changelog
Changes should go to 0.21.0 section

YAGregor added a commit to YAGregor/tortoise-orm that referenced this pull request Apr 27, 2024
@YAGregor YAGregor force-pushed the feature-async-default-value branch from 246314f to b6864a2 Compare April 27, 2024 07:31
CHANGELOG.rst Outdated
@@ -1333,3 +1333,4 @@ Docs/examples:
await Tournament.filter(
events__name__in=['1', '3']
).order_by('-events__participants__name').distinct()
* Allow field's default keyword to be async function (#1498)
Copy link
Member

Choose a reason for hiding this comment

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

Seems like this entry is in wrong place

@YAGregor YAGregor force-pushed the feature-async-default-value branch from b6864a2 to 12b66d9 Compare April 27, 2024 09:40
@abondar abondar merged commit b72c175 into tortoise:develop Apr 27, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants