Comments (7)
Yeah, it's a good idea. I think there are two options.
Modifying the where method
The where
method could be modified, so it accepts multiple arguments.
# 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 method
I like the idea of adding get
.
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 MyModel.objects.get(id=1)
in Django. What do you think?
from piccolo.
I think the 2nd
method is best since it does not break existing functionality.
I think it could be simply implemented by doing something along the lines of:
@classmethod
def get(cls, **kwargs):
parsed_args = # Parse the args
return Objects().where(AND(parsed_args))
from piccolo.
The first method could be implemented in a backwards compatible way. The where
signature would just be changed from:
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?
from piccolo.
Hmmm, yes, I think this is exactly what we need although .first()
can be potentially problematic. Why not just return the entire queryset?
from piccolo.
It's just to mimic how get
works in Django, and also in Python in general.
For example:
>>> foo = {'a': 1}
>>> foo.get('a')
1
>>> foo.get('b')
None
If all results are needed, it could be queried like await MyModel.objects().where(a=1)
. Or maybe a method name other than get
to avoid confusion.
from piccolo.
Okay, sounds great. Do you need a PR? Working on one right now.
Okay, modified #83 to accomodate this as well
from piccolo.
There are now get
and get_or_create
methods in master now - I'll close this for now, but feel free to reopen if you feel anything needs adding.
from piccolo.
Related Issues (20)
- Fix `load_json` on joined tables
- Support arrays of timestamp / timestamptz / date / time in SQLite
- cockroachDB extremely long query due to Array(UUID()) HOT 3
- Upgrade CockroachDB
- Is there a way to truncate the table? HOT 3
- Add support for `CAST` to convert between types
- Add more operators to `QueryString`
- Add basic math functions
- Improve array serialisation in `get_sql_value`
- Replace deprecated `@abstractproperty`
- Add `not_any` method to `Array`
- Add `Concat` function
- Replace deprecated `startup` and `shutdown` events in asgi templates HOT 1
- Document how to achieve a `OneToOneField` in Piccolo
- Use `TableTest` to cut down boiler plate in tests
- Be able to hardcode migrations as fake
- Reverse relations fetching HOT 11
- Fix typo in `Email` docstring
- Improve `LazyTableReference`
- Add `Album` table to playground
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from piccolo.