Re: A.objects.getdefault

2013-01-26 Thread Selwin Ong
Hi Wim, I think there's a slight misunderstanding here. I completely agree with you that .first() and .last() should return None if there's no matching row :). The syntax I proposed: User.objects.exclude(a=b).filter(c=d).first('id') # Returns None if there's no matching row User.objec

Re: A.objects.getdefault

2013-01-26 Thread Mike Fogel
+1 to replacing earliest() and latest() with order_by('field').first() and .last(), respectively. I'm not a fan of the implied semantics of 'earliest' and 'latest' - as if they only worked with time-based fields. Mike On Tuesday, January 22, 2013 3:03:22 PM UTC-8, Wim Feijen wrote: > > Hi Selwi

Re: A.objects.getdefault

2013-01-22 Thread Wim Feijen
Hi Selwin and Anssi, Anssi, thanks for improving the patch and getting it ready for commit! Selwin, you are right that .filter() and .first() are very similar. Rationale for .first() is to get rid of the pattern try: instance = ModelX.objects.get except ObjectDoesNotExist: instance =

Re: A.objects.getdefault

2013-01-20 Thread Selwin Ong
Hi Anssi, Shouldn't first() and last() raise an exception if no ordering is specified? This keeps it consistent with latest() which requires the user to explicitly specify what field it wants to use as ordering. Also, like you mentioned, IMHO these APIs are too similar (first, earliest, last a

Re: A.objects.getdefault

2013-01-19 Thread Anssi Kääriäinen
On 10 tammi, 09:27, Wim Feijen wrote: > Hi, > > Ticket 19326 has been marked as ready for check-in for some time. Can > some-one have a look at it? > >  https://code.djangoproject.com/ticket/19326 > > Thanks, > > Wim I did some more polish to the patch. There is now also .last() method, and if th

Re: A.objects.getdefault

2013-01-09 Thread Wim Feijen
Hi, Ticket 19326 has been marked as ready for check-in for some time. Can some-one have a look at it? https://code.djangoproject.com/ticket/19326 Thanks, Wim Op donderdag 29 november 2012 23:45:38 UTC+1 schreef Wim Feijen het volgende: > > Hi Anssi, > > When I thought about it, the most pr

Re: A.objects.getdefault

2012-11-29 Thread Wim Feijen
Hi Anssi, When I thought about it, the most prominent usecase seemed to me of getting one of a set of many and expecting just one result. In that case, get_or_none would suffice. However, as a method name, I prefer first() for being concise. For me it is ok to just have the first() method wit

Re: A.objects.getdefault

2012-11-28 Thread Anssi Kääriäinen
On 29 marras, 01:13, Wim Feijen wrote: > Hi, the patch has been updated and now works. > > Still, feedback would be appreciated. So, Anssi, Jacob? Apart of some whitespace errors the patch looks good to me. There isn't last() method in the patch. Implementing one is going to be a little more cha

Re: A.objects.getdefault

2012-11-28 Thread Wim Feijen
Hi, the patch has been updated and now works. Still, feedback would be appreciated. So, Anssi, Jacob? - Wim Op maandag 19 november 2012 22:48:36 UTC+1 schreef Wim Feijen het volgende: > > Hi, > > I do like the first() method and went ahead and *tried* to implement it. > > Ticket: > https://code

Re: A.objects.getdefault

2012-11-19 Thread Wim Feijen
Hi, I do like the first() method and went ahead and *tried* to implement it. Ticket: https://code.djangoproject.com/ticket/19326 Patch, including tests and doc changes: https://code.djangoproject.com/attachment/ticket/19326/19326.diff Unfortunately, tests fail. Probably ordering is wrong, or I

Re: A.objects.getdefault

2012-10-15 Thread Michael Hudson-Doyle
On 16 October 2012 07:19, Jacob Kaplan-Moss wrote: > On Mon, Oct 15, 2012 at 7:42 AM, Anssi Kääriäinen > wrote: >> In the end this is a decision with almost no technical considerations and a >> lot of "good taste" considerations. So, this seems like BDFL area. > > Hi! > > After thinking a bit, I'

Re: A.objects.getdefault

2012-10-15 Thread Jacob Kaplan-Moss
On Mon, Oct 15, 2012 at 7:42 AM, Anssi Kääriäinen wrote: > In the end this is a decision with almost no technical considerations and a > lot of "good taste" considerations. So, this seems like BDFL area. Hi! After thinking a bit, I'm +1 on the idea, and I think `Queryset.first()` is the right na

Re: A.objects.getdefault

2012-10-15 Thread Tom Evans
On Mon, Oct 15, 2012 at 1:42 PM, Anssi Kääriäinen wrote: > On 10/15/2012 03:13 PM, Ole Laursen wrote: > > On Friday, October 12, 2012 3:35:53 PM UTC+2, Chris Wilson wrote: >> >> I'm strongly in favour of a simple, obvious way to do the common thing, >> which is to return None if the object doesn't

Re: A.objects.getdefault

2012-10-15 Thread Anssi Kääriäinen
On 10/15/2012 03:13 PM, Ole Laursen wrote: On Friday, October 12, 2012 3:35:53 PM UTC+2, Chris Wilson wrote: I'm strongly in favour of a simple, obvious way to do the common thing, which is to return None if the object doesn't exist, instead of throwing an exception. My prefe

Re: A.objects.getdefault

2012-10-15 Thread Ole Laursen
On Friday, October 12, 2012 3:35:53 PM UTC+2, Chris Wilson wrote: > > I'm strongly in favour of a simple, obvious way to do the common thing, > which is to return None if the object doesn't exist, instead of throwing > an exception. My preferred method names would be .nget(), .get_or_none() > or

Re: A.objects.getdefault

2012-10-12 Thread Chris Wilson
Hi all, On Thu, 11 Oct 2012, Daniel Moisset wrote: obj, = SomeModel.objects.filter(foo='bar') or [None] Daniel's solution is elegant, but far from clear or clean. I'm strongly in favour of a simple, obvious way to do the common thing, which is to return None if the object doesn't exist, ins

Re: A.objects.getdefault

2012-10-11 Thread Daniel Moisset
On Thu, Oct 11, 2012 at 2:00 PM, Marijonas Petrauskas wrote: > You can use: > > obj = next(iter(SomeModel.objects.filter(foo='bar')), None) > > The 'iter' part is not particularly elegant, but it's the only one-liner > known to me. > obj, = SomeModel.objects.filter(foo='bar') or [None] but we're

Re: A.objects.getdefault

2012-10-11 Thread Marijonas Petrauskas
You can use: obj = next(iter(SomeModel.objects.filter(foo='bar')), None) The 'iter' part is not particularly elegant, but it's the only one-liner known to me. -- Marijonas On Thu, Oct 11, 2012 at 3:45 PM, Ole Laursen wrote: > On Tuesday, October 9, 2012 7:15:55 PM UTC+2, ptone wrote: >> >> E

Re: A.objects.getdefault

2012-10-11 Thread Ole Laursen
On Tuesday, October 9, 2012 7:15:55 PM UTC+2, ptone wrote: > > Earlier discussion > > https://groups.google.com/forum/?fromgroups=#!topic/django-developers/Saa5nbzqQ2Q > This was the thread I referred to. If was from 2006 and ended up being about something else. > tickets: > https://code.djang

Re: A.objects.getdefault

2012-10-09 Thread Anssi Kääriäinen
On 10 loka, 00:49, Michael Hudson-Doyle wrote: > > OK, it seems .get_or_none() method is out. I can see the point of that > > decision, there are a lot of similar methods that could be added using > > the convenience argument (like .first() in conjunction > > with .latest()). > > > But, how about

Re: A.objects.getdefault

2012-10-09 Thread Michael Hudson-Doyle
On 10 October 2012 10:29, Anssi Kääriäinen wrote: > On 9 loka, 20:15, ptone wrote: >> Unsurprisingly - this has come up before: >> >> Earlier >> discussionhttps://groups.google.com/forum/?fromgroups=#!topic/django-developers... >> >> tickets:https://code.djangoproject.com/ticket/17546https://cod

Re: A.objects.getdefault

2012-10-09 Thread Anssi Kääriäinen
On 9 loka, 20:15, ptone wrote: > Unsurprisingly - this has come up before: > > Earlier > discussionhttps://groups.google.com/forum/?fromgroups=#!topic/django-developers... > > tickets:https://code.djangoproject.com/ticket/17546https://code.djangoproject.com/ticket/2659https://code.djangoproject.c

Re: A.objects.getdefault

2012-10-09 Thread Tino de Bruijn
+1 on a method that returns non upon not finding. We don't need to do: try: val = some_dict.get('key') except KeyError: val = None either. A nget or get_or_none would save me a lot of boilerplate code in my views as well. Tino On Tue, Oct 9, 2012 at 8:48 PM, Wim Feijen wrote: > For me,

Re: A.objects.getdefault

2012-10-09 Thread Wim Feijen
For me, get_or_none would prevent a lot of boilerplate code in my views, so I am in favor of adding the method. Wim Op dinsdag 9 oktober 2012 19:15:55 UTC+2 schreef ptone het volgende: > > Unsurprisingly - this has come up before: > > Earlier discussion > > https://groups.google.com/forum/?fromg

Re: A.objects.getdefault

2012-10-09 Thread ptone
Unsurprisingly - this has come up before: Earlier discussion https://groups.google.com/forum/?fromgroups=#!topic/django-developers/Saa5nbzqQ2Q tickets: https://code.djangoproject.com/ticket/17546 https://code.djangoproject.com/ticket/2659 https://code.djangoproject.com/ticket/11352 All the ticke

Re: A.objects.getdefault

2012-10-09 Thread ptone
M UTC-7, Ole Laursen wrote: > > Hi! > > What do people think of > > A.objects.getdefault(slug="hello") # returns None if slug doesn't exist > A.objects.getdefault(slug="hello", default=A()) # returns empty object > if slug doesn't exis

Re: A.objects.getdefault

2012-10-09 Thread Carlos Goldsmith
On Tue, Oct 9, 2012 at 7:29 AM, Anssi Kääriäinen wrote: > On 9 loka, 17:05, Ole Laursen wrote: > > Hi! > > > > What do people think of > > > > A.objects.getdefault(slug="hello") # returns None if slug doesn't > exist > > A.objec

Re: A.objects.getdefault

2012-10-09 Thread Anssi Kääriäinen
On 9 loka, 17:05, Ole Laursen wrote: > Hi! > > What do people think of > >   A.objects.getdefault(slug="hello")  # returns None if slug doesn't exist >   A.objects.getdefault(slug="hello", default=A())  # returns empty object > if slug doesn't

A.objects.getdefault

2012-10-09 Thread Ole Laursen
Hi! What do people think of A.objects.getdefault(slug="hello") # returns None if slug doesn't exist A.objects.getdefault(slug="hello", default=A()) # returns empty object if slug doesn't exist I find that in practice, most of the time it would be better to