Coder Social home page Coder Social logo

Comments (7)

dantownsend avatar dantownsend commented on August 28, 2024

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.

coder3112 avatar coder3112 commented on August 28, 2024

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.

dantownsend avatar dantownsend commented on August 28, 2024

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.

coder3112 avatar coder3112 commented on August 28, 2024

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.

dantownsend avatar dantownsend commented on August 28, 2024

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.

coder3112 avatar coder3112 commented on August 28, 2024

Okay, sounds great. Do you need a PR? Working on one right now.

Okay, modified #83 to accomodate this as well

from piccolo.

dantownsend avatar dantownsend commented on August 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.