Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
proposal: get_or_none QuerySet method
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  22 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Gary Wilson  
View profile  
 More options Dec 18 2006, 4:40 pm
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Mon, 18 Dec 2006 13:40:15 -0800
Local: Mon, Dec 18 2006 4:40 pm
Subject: proposal: get_or_none QuerySet method
I often find myself writing code like:

try:
    user = User.objects.get(pk=user_id)
except User.DoesNotExist:
    user = None

What do you think about adding a get_or_none QuerySet method?

def get_or_none(self, **kwargs):
    try:
        return self.get(**kwargs)
    except self.model.DoesNotExist:
        return None


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Waylan Limberg  
View profile  
 More options Dec 18 2006, 5:08 pm
From: "Waylan Limberg" <way...@gmail.com>
Date: Mon, 18 Dec 2006 17:08:54 -0500
Local: Mon, Dec 18 2006 5:08 pm
Subject: Re: proposal: get_or_none QuerySet method
On 12/18/06, Gary Wilson <gary.wil...@gmail.com> wrote:

Or how about using something similar to QuearyDict's get(key,
default)? That way the fallback is not restricted to `None`. So
something like this:

user = User.objects.get(pk=user_id, default=None)

Of course, I think that would probably be significantly more work than
your little wrapper function.
--
----
Waylan Limberg
way...@gmail.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Honza Král  
View profile  
 More options Dec 18 2006, 5:14 pm
From: "Honza Král" <honza.k...@gmail.com>
Date: Mon, 18 Dec 2006 23:14:31 +0100
Local: Mon, Dec 18 2006 5:14 pm
Subject: Re: proposal: get_or_none QuerySet method
On 12/18/06, Waylan Limberg <way...@gmail.com> wrote:

note neccessarily (beware, quick&ugly proof of concept):
Index: django/db/models/query.py
===================================================================
--- django/db/models/query.py   (revision 4227)
+++ django/db/models/query.py   (working copy)
@@ -12,6 +12,8 @@

 # The string constant used to separate query parts
 LOOKUP_SEPARATOR = '__'
+# for get(default)
+NOT_SET='WHATEVER'

 # The list of valid query types
 QUERY_TERMS = (
@@ -202,7 +204,8 @@
             cursor.execute("SELECT COUNT(*)" + sql, params)
         return cursor.fetchone()[0]

-    def get(self, *args, **kwargs):
+
+    def get(self, default=NOT_SET, *args, **kwargs):
         "Performs the SELECT and returns a single object matching the
given keyword arguments."
         clone = self.filter(*args, **kwargs)
         # clean up SQL by removing unneeded ORDER BY
@@ -210,6 +213,8 @@
             clone._order_by = ()
         obj_list = list(clone)
         if len(obj_list) < 1:
+            if default != NOT_SET:
+                return default
             raise self.model.DoesNotExist, "%s matching query does
not exist." % self.model._meta.object_name
         assert len(obj_list) == 1, "get() returned more than one %s
-- it returned %s! Lookup parameters were %s" %
(self.model._meta.object_name, len(obj_list), kwargs)
         return obj_list[0]

> --
> ----
> Waylan Limberg
> way...@gmail.com

--
Honza Král
E-Mail: Honza.K...@gmail.com
ICQ#:   107471613
Phone:  +420 606 678585

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SmileyChris  
View profile  
 More options Dec 18 2006, 7:04 pm
From: "SmileyChris" <smileych...@gmail.com>
Date: Tue, 19 Dec 2006 00:04:21 -0000
Local: Mon, Dec 18 2006 7:04 pm
Subject: Re: proposal: get_or_none QuerySet method
On Dec 19, 11:08 am, "Waylan Limberg" <way...@gmail.com> wrote:

> something like this:

> user = User.objects.get(pk=user_id, default=None)

Except this would break (or at least limit the functionality of)
objects which use "default" as a field name.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Waylan Limberg  
View profile  
 More options Dec 18 2006, 9:35 pm
From: "Waylan Limberg" <way...@gmail.com>
Date: Mon, 18 Dec 2006 21:35:05 -0500
Local: Mon, Dec 18 2006 9:35 pm
Subject: Re: proposal: get_or_none QuerySet method
Nice job Honza. I was about to suggest just about the same, but
hesitated because of the *args and **kwargs. I just wasn't sure why
that made me hesitate.

On 12/18/06, SmileyChris <smileych...@gmail.com> wrote:

> On Dec 19, 11:08 am, "Waylan Limberg" <way...@gmail.com> wrote:
> > something like this:

> > user = User.objects.get(pk=user_id, default=None)

> Except this would break (or at least limit the functionality of)
> objects which use "default" as a field name.

Now I know why. Good call SmileyChris. Unfortunately I have no ideas
for a workaround.

--
----
Waylan Limberg
way...@gmail.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Wilson  
View profile  
 More options Dec 20 2006, 11:00 am
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Wed, 20 Dec 2006 16:00:46 -0000
Local: Wed, Dec 20 2006 11:00 am
Subject: Re: proposal: get_or_none QuerySet method

SmileyChris wrote:
> On Dec 19, 11:08 am, "Waylan Limberg" <way...@gmail.com> wrote:
> > something like this:

> > user = User.objects.get(pk=user_id, default=None)

> Except this would break (or at least limit the functionality of)
> objects which use "default" as a field name.

Of course, this didn't stop get_or_create(), which uses "defaults" as a
parameter.  If you had an object with an attribute named "default" then
you could still do something like:

obj = MyModel.objects.get(default__exact=42, default=None)

But get_or_create() was new functionality and this would be changing
existing functionality.  If that is unacceptable, then maybe a
get_or_default() that optionally takes the "default" parameter, using
None if no "default" specified.

So...
MyModel.objects.get_or_default(pk=1)
would be equivalent to
MyModel.objects.get_or_default(pk=1, default=None)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joseph Perla  
View profile  
 More options Dec 20 2006, 11:14 am
From: "Joseph Perla" <josephpe...@gmail.com>
Date: Wed, 20 Dec 2006 11:14:38 -0500
Local: Wed, Dec 20 2006 11:14 am
Subject: Re: proposal: get_or_none QuerySet method

There should also be an update_or_create() function.  I often find myself
get_or_creating, then updating the object's dict if it wasnt just created,
then saving.  This can easily be expressed more clearly in one line with
this function.
j

On 12/20/06, Gary Wilson <gary.wil...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brantley Harris  
View profile  
 More options Dec 20 2006, 2:19 pm
From: "Brantley Harris" <deadwis...@gmail.com>
Date: Wed, 20 Dec 2006 13:19:27 -0600
Local: Wed, Dec 20 2006 2:19 pm
Subject: Re: proposal: get_or_none QuerySet method
On 12/18/06, Gary Wilson <gary.wil...@gmail.com> wrote:

> What do you think about adding a get_or_none QuerySet method?

+1

On 12/20/06, Joseph Perla <josephpe...@gmail.com> wrote:

> There should also be an update_or_create() function.

+1

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joseph Perla  
View profile  
 More options Dec 20 2006, 11:42 pm
From: "Joseph Perla" <josephpe...@gmail.com>
Date: Wed, 20 Dec 2006 23:42:00 -0500
Subject: Re: proposal: get_or_none QuerySet method

Also, here's the pseudo-ish code for it:

def update_or_create( --same arguments as get_or_create()-- ):
     object, created = Class.objects.get_or_create( --args above-- )
     if not created:
          object.__dict__.update(defaults)
          object.save()
     return object

Well, Class is self or this or whatever it is in python/djangomanagers
code.  I'm also not sure how to do the crazy **kwargs stuff.  The rest is
clear: if you didn't create it, just update it, save it, and return.
j

On 12/20/06, Joseph Perla <josephpe...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Wilson  
View profile  
 More options Dec 22 2006, 12:58 am
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Fri, 22 Dec 2006 05:58:31 -0000
Local: Fri, Dec 22 2006 12:58 am
Subject: Re: proposal: get_or_none QuerySet method

Joseph Perla wrote:
> There should also be an update_or_create() function.  I often find myself
> get_or_creating, then updating the object's dict if it wasnt just created,
> then saving.  This can easily be expressed more clearly in one line with
> this function.

This gave me the idea of an update() method for model instances.  I
have created a ticket for this with code, documentation, and tests.

http://code.djangoproject.com/ticket/3180


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Wilson  
View profile  
 More options Dec 22 2006, 2:07 am
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Fri, 22 Dec 2006 07:07:14 -0000
Local: Fri, Dec 22 2006 2:07 am
Subject: Re: proposal: get_or_none QuerySet method

Joseph Perla wrote:
> Also, here's the pseudo-ish code for it:

> def update_or_create( --same arguments as get_or_create()-- ):
>      object, created = Class.objects.get_or_create( --args above-- )
>      if not created:
>           object.__dict__.update(defaults)
>           object.save()
>      return object

> Well, Class is self or this or whatever it is in python/djangomanagers
> code.  I'm also not sure how to do the crazy **kwargs stuff.  The rest is
> clear: if you didn't create it, just update it, save it, and return.

I have created a ticket for this too and have attached some code:

http://code.djangoproject.com/ticket/3182


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Wilson  
View profile  
 More options Dec 24 2006, 5:50 pm
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Sun, 24 Dec 2006 14:50:10 -0800
Local: Sun, Dec 24 2006 5:50 pm
Subject: Re: proposal: get_or_none QuerySet method

Gary Wilson wrote:
> I have created a ticket for this too and have attached some code:

> http://code.djangoproject.com/ticket/3182

Documentation and tests attached too now.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacob Kaplan-Moss  
View profile  
 More options Dec 24 2006, 10:35 pm
From: Jacob Kaplan-Moss <ja...@jacobian.org>
Date: Sun, 24 Dec 2006 21:35:33 -0600
Local: Sun, Dec 24 2006 10:35 pm
Subject: Re: proposal: get_or_none QuerySet method
On 12/24/06 4:50 PM, Gary Wilson wrote:

>> I have created a ticket for this too and have attached some code:

>> http://code.djangoproject.com/ticket/3182

> Documentation and tests attached too now.

Hm - I get a whole bunch of failures when I apply the patch.  Looking closer,
you say it depends on an ``update()`` patch in #3181, but I don't see any such
patch attached to #3181.

Can you update #3182 to include this other patch (wherever it lives), and
close the other ticket as a dup of #3182? Once that's done I'll go ahead and
check this in.

Jacob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Wilson  
View profile  
 More options Dec 25 2006, 12:15 am
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Sun, 24 Dec 2006 21:15:21 -0800
Local: Mon, Dec 25 2006 12:15 am
Subject: Re: proposal: get_or_none QuerySet method

Jacob Kaplan-Moss wrote:
> Hm - I get a whole bunch of failures when I apply the patch.  Looking closer,
> you say it depends on an ``update()`` patch in #3181, but I don't see any such
> patch attached to #3181.

Sorry about that, it should be #3180.

> Can you update #3182 to include this other patch (wherever it lives), and
> close the other ticket as a dup of #3182? Once that's done I'll go ahead and
> check this in.

Well, the two tickets are different functionality.  I could create a
single patch for both, the only thing was that in the patch for #3180 I
added an "Updating objects" to the db api documentation, but then
realized that it should probably be integrated into the "Saving changes
to objects" section.  I haven't spent the time to do that yet.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacob Kaplan-Moss  
View profile  
 More options Dec 25 2006, 1:05 am
From: Jacob Kaplan-Moss <ja...@jacobian.org>
Date: Mon, 25 Dec 2006 00:05:37 -0600
Local: Mon, Dec 25 2006 1:05 am
Subject: Re: proposal: get_or_none QuerySet method
On 12/24/06 11:15 PM, Gary Wilson wrote:

> Well, the two tickets are different functionality.  I could create a
> single patch for both, the only thing was that in the patch for #3180 I
> added an "Updating objects" to the db api documentation, but then
> realized that it should probably be integrated into the "Saving changes
> to objects" section.  I haven't spent the time to do that yet.

Yeah, they're different... but I think they make the most sense if they go in
as a unit.  I'd prefer a single patch (and tests, docs, etc.) if you're up to it.

Jacob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Wilson  
View profile  
 More options Dec 25 2006, 1:18 am
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Sun, 24 Dec 2006 22:18:53 -0800
Local: Mon, Dec 25 2006 1:18 am
Subject: Re: proposal: get_or_none QuerySet method

Jacob Kaplan-Moss wrote:
> On 12/24/06 11:15 PM, Gary Wilson wrote:
> > Well, the two tickets are different functionality.  I could create a
> > single patch for both, the only thing was that in the patch for #3180 I
> > added an "Updating objects" to the db api documentation, but then
> > realized that it should probably be integrated into the "Saving changes
> > to objects" section.  I haven't spent the time to do that yet.

> Yeah, they're different... but I think they make the most sense if they go in
> as a unit.  I'd prefer a single patch (and tests, docs, etc.) if you're up to it.

Sure, I'll work on the documentation fixes and let you know when done.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Wilson  
View profile  
 More options Dec 26 2006, 1:10 am
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Tue, 26 Dec 2006 06:10:05 -0000
Local: Tues, Dec 26 2006 1:10 am
Subject: Re: proposal: get_or_none QuerySet method

Gary Wilson wrote:
> Jacob Kaplan-Moss wrote:
> > On 12/24/06 11:15 PM, Gary Wilson wrote:
> > > Well, the two tickets are different functionality.  I could create a
> > > single patch for both, the only thing was that in the patch for #3180 I
> > > added an "Updating objects" to the db api documentation, but then
> > > realized that it should probably be integrated into the "Saving changes
> > > to objects" section.  I haven't spent the time to do that yet.

> > Yeah, they're different... but I think they make the most sense if they go in
> > as a unit.  I'd prefer a single patch (and tests, docs, etc.) if you're up to it.

> Sure, I'll work on the documentation fixes and let you know when done.

Ok.  I reworked the documentation, rolled both patches into one, and
attached it to #3182.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Wilson  
View profile  
 More options Jan 3 2007, 7:46 pm
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Wed, 03 Jan 2007 16:46:19 -0800
Local: Wed, Jan 3 2007 7:46 pm
Subject: Re: proposal: get_or_none QuerySet method
Ok, after getting sidetracked by update() and update_or_create(), back
to the topic of the OP.   Here are some options:

1) Add a get_or_none() method that returns None if object does not
exist.

2) Add a get_or_default() method that accepts a "default" keyword
argument, the value of which gets returned if object does not exist.
If a default is not specified then None gets returned by default.

3) Modify the current get() method to accept a "default" keyword
argument as in option 2), except that instead of returning None if
"default" is not specified, a DoesNotExist exception is raised like
normal.  To return None, you would have to explicitly say
"default=None".

Note that this would also be backwards incompatible in that models with
a field named "default" would have to use "default__exact=..." instead
of "default=..." as with get_or_create().  Looking further, it looks
like functions that use get() might also have to change slightly, i.e.
get_or_create().

Comments/votes/suggestions?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Waylan Limberg  
View profile  
 More options Jan 3 2007, 8:56 pm
From: "Waylan Limberg" <way...@gmail.com>
Date: Wed, 3 Jan 2007 20:56:04 -0500
Local: Wed, Jan 3 2007 8:56 pm
Subject: Re: proposal: get_or_none QuerySet method
On 1/3/07, Gary Wilson <gary.wil...@gmail.com> wrote:

I like 3, but considering the potential complications 2 may make more
sense. Either way, I don't see the value in 1 as in either 2 or 3, the
default can still be set to None.

--
----
Waylan Limberg
way...@gmail.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Wilson  
View profile  
 More options Jan 14 2007, 9:48 pm
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Mon, 15 Jan 2007 02:48:57 -0000
Local: Sun, Jan 14 2007 9:48 pm
Subject: Re: proposal: get_or_none QuerySet method
On Dec 24 2006, 9:35 pm, Jacob Kaplan-Moss <j...@jacobian.org> wrote:

> On 12/24/06 4:50 PM, Gary Wilson wrote:

> >> I have created a ticket for this too and have attached some code:

> >>http://code.djangoproject.com/ticket/3182

> > Documentation and tests attached too now.Hm - I get a whole bunch of failures when I apply the patch.  Looking closer,
> you say it depends on an ``update()`` patch in #3181, but I don't see any such
> patch attached to #3181.

> Can youupdate#3182 to include this other patch (wherever it lives), and
> close the other ticket as a dup of #3182? Once that's done I'll go ahead and
> check this in.

> Jacob

Any other requests for the new, combined patch?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
whiteinge  
View profile  
 More options Mar 10 2007, 10:54 am
From: "whiteinge" <whitei...@gmail.com>
Date: Sat, 10 Mar 2007 15:54:10 -0000
Local: Sat, Mar 10 2007 10:54 am
Subject: Re: proposal: get_or_none QuerySet method
If someone has a moment I would appreciate hearing a status update for
this ticket--it reports ready for checkin, will it still be included
in Django?

I tested the patch and it works as advertised, also the docs are well
written. This is functionality I'm looking forward to.

<http://code.djangoproject.org/ticket/3182>

Thanks!
- whiteinge


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Malcolm Tredinnick  
View profile  
 More options Mar 10 2007, 2:27 pm
From: Malcolm Tredinnick <malc...@pointy-stick.com>
Date: Sun, 11 Mar 2007 06:27:55 +1100
Local: Sat, Mar 10 2007 2:27 pm
Subject: Re: proposal: get_or_none QuerySet method

On Sat, 2007-03-10 at 15:54 +0000, whiteinge wrote:
> If someone has a moment I would appreciate hearing a status update for
> this ticket--it reports ready for checkin, will it still be included
> in Django?

> I tested the patch and it works as advertised, also the docs are well
> written. This is functionality I'm looking forward to.

> <http://code.djangoproject.org/ticket/3182>

It will probably be applied at some point. It is one of a number of open
tickets. Sorry, but there are lots of things we want to work on all at
the same time.

Malcolm


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google