-
Notifications
You must be signed in to change notification settings - Fork 7
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
Retrieving a nested model asynchronously is unnecessary #13
Comments
Foreign keys are awaitable because you may load them on demand (like peewee does): for user in await User.select():
role = await user.role # load the role (yes the example is not optimised)
print(f'user {user.id} have role {role.name}') In case you had preloaded related models, like in your example before for user in await User.select(User, Role).join(Role, src=User):
role = User.role.fetch(user) # the role has to be preloaded
print(f'user {user.id} have role {role.name}') |
Your code example does exactly what I want. But in fact, I don’t need to work directly with the nested model, but I need Now it crashes with the following errors:
That's why I wanted to change the main field so that the internals of peewee work correctly not only at the level of my application, but also in other functions from the playhouse. |
That's because |
I have these models:
I want to query the database and get a list of models and then use the nested model somehow. I want to make a request like this:
The code should now look like this:
This code looks ugly and inconvenient. In addition, receiving model Role from the user object is internally synchronous (because peewee has already enriched the main model with a nested model).
This is because AIOForeignKeyField overrides
accessor_class
so that the.get_rel_instance()
function becomes asynchronous.But there is no need for this in my request. All information about the nested model is already in
instance.__rel__
and is given synchronously.I did this crutch for myself:
In this case, I can access the nested model directly without "await":
Proposed solution
Make field overriding optional through some parameter in this function:
Then I can choose for myself when the fields need to be redefined, and when I want to use the original peewee fields:
The text was updated successfully, but these errors were encountered: