Skip to content

Acessing property_fields that queries the database asynchronously #443

Answered by collerek
igormorgado asked this question in Q&A
Discussion options

You must be logged in to vote

Unfortunately, both python properties and ormar property_fields are sync only, as they are used in dumping the data into the dictionary in pydantic which is only synchronous.

So in your case you can filter already loaded data:

@orm.property_field
def rare_items_owned(self):
    # items have to be already loaded at this point
    return len([x for x in self.items if x.is_rare])

Otherwise, you have to fetch the loop and wrap the coroutine in asyncio.run() call. (that should work)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@igormorgado
Comment options

Answer selected by igormorgado
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants