-
Notifications
You must be signed in to change notification settings - Fork 90
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
[ENHANCEMENT] Create a proxy for where in the form of get #84
Comments
Yeah, it's a good idea. I think there are two options. Modifying the where methodThe # What's currently possible (all of these are equivalent):
await MyModel.select().where((MyModel.a == 1) & (MyModel.b == 2))
await MyModel.select().where(And(MyModel.a == 1, MyModel.b == 2))
await MyModel.select().where(MyModel.a == 1).where(MyModel.b == 2)
# What could be added:
# 1. Multiple arguments would behave the same as an AND, which does clean up the syntax a bit.
await MyModel.select().where(MyModel.a == 1, MyModel.b == 2)
# 2. Could also accept kwargs, which are converted to `MyModel.a == 1`.
await MyModel.select().where(a=1, b=2) Adding a get methodI like the idea of adding await MyModel.get(id=1)
# It's the equivalent of:
await MyModel.objects().where(MyModel.id == 1).first() So it does save quite a bit of boiler plate code. I think this method would be for returning a single matching row, in the same way as |
I think the @classmethod
def get(cls, **kwargs):
parsed_args = # Parse the args
return Objects().where(AND(parsed_args)) |
The first method could be implemented in a backwards compatible way. The def where(self, where: Combinable):
self.where_delegate.where(where)
return self To: def where(self, *where: Combinable, **kwargs):
for i in where:
self.where_delegate.where(i)
for key, value in kwargs.items():
column = self._meta.get_column_by_name(key)
self.where_delegate.where(Where(column=column, value=value, operator=Equal))
return self If implementing option 2, it would then just be: @classmethod
def get(cls, *where: Combinable, **kwargs):
return self.objects().where(*args, **kwargs).first() What do you think? |
Hmmm, yes, I think this is exactly what we need although |
It's just to mimic how For example: >>> foo = {'a': 1}
>>> foo.get('a')
1
>>> foo.get('b')
None If all results are needed, it could be queried like |
Okay, sounds great. Do you need a PR? Working on one right now. Okay, modified #83 to accomodate this as well |
There are now |
Problem
Write now, the code I have to write becomes lengthy and hard to read if there are multiple conditions.
Example:
The only thing I can do to make it better is:
This doesn't look quite as intuitive and is super difficult to read in case of larger queries where there are multiple
OR
andAND
at different complexities.Solution I would like
A simple nice proxy to
where
in the form ofget
. (like save is to insert/update)Do you think this should be worked upon. Willing to submit a PR
The text was updated successfully, but these errors were encountered: