tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.58k stars 378 forks source link

Feature: async default value #1498

Closed YAGregor closed 5 months ago

YAGregor commented 11 months ago

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:

long2ice commented 11 months ago

Thanks! LGTM. Please update changelog also.

coveralls commented 11 months ago

Pull Request Test Coverage Report for Build 8858936385

Details


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

💛 - Coveralls
YAGregor commented 11 months ago

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

abondar commented 5 months ago

@YAGregor hi!

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