Re: shortest way to recover from a QuerySet "object not found"
If the question is answered to your satisfaction, why don't we drop the thread now and avoid the personal stuff? There is nothing to be gained by arguing with a respected core Django developer, and nothing the rest of us will learn from it. Thanks, Shawn -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: shortest way to recover from a QuerySet "object not found"
> The only condescension I've seen in this thread is from you. And, to > be fair, if I wanted to be condescending I'd have simply pointed you > at Tony Hoare's explanation of null values That's why I said NullObject, in the first post. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: shortest way to recover from a QuerySet "object not found"
On Thu, Mar 4, 2010 at 1:43 PM, Phlipwrote: > And again the condescension. As a programmer, I should be able to > easily chose between statements that throw and ones that efficiently > deal with branching conditions. A record-not-found is not a crisis, it > is just a branching condition. 5 excess lines of code do not make my > program more robust. The only condescension I've seen in this thread is from you. And, to be fair, if I wanted to be condescending I'd have simply pointed you at Tony Hoare's explanation of null values (which he originally introduced into ALGOL, way back at the dawn of time) as a "billion-dollar mistake" ;) In general, though, Django optimizes for the most common cases. Experience has shown that, in the case of a record not being found, there are two things people most often want to do: 1. Bail out and return an HTTP 404 response. 2. Create a record which matched the specified conditions. As a result, Django provides one-line shortcuts which implement both of those use cases (get_object_or_404() and get_or_create()). Creating a null object to pass around, meanwhile, is less common and -- as mentioned -- is more of an anti-pattern to be discouraged. As for why get() raises an exception, well, it *is* an exceptional case; usually, when you're that specific in asking for an object, you expect it to be there. > Oh, look! "123"[:7] ! The string doesn't have 7 characters! Gee, > shouldn't that throw an exception? See my comments above about condescension. -- "Bureaucrat Conrad, you are technically correct -- the best kind of correct." -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: shortest way to recover from a QuerySet "object not found"
Just create your own Manager and override the default (named 'objects') in your models. Have 'get' behave any way you like. http://docs.djangoproject.com/en/1.1/topics/db/managers/ Shawn -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: shortest way to recover from a QuerySet "object not found"
> Just create your own Manager and override the default (named 'objects') in > your models. Have 'get' behave any way you like. > > http://docs.djangoproject.com/en/1.1/topics/db/managers/ > > Shawn Ding! http://docs.djangoproject.com/en/1.1/topics/db/managers/#adding-extra-manager-methods luv that Open Closed Principle, huh? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: shortest way to recover from a QuerySet "object not found"
> Most likely, yes. And those people, believe it or not, designed > Django's APIs based on their experiences. This is why there are > shortcuts available, like the get_or_create() method (which fetches an > object or, if none matches, creates a valid one, populated with > default values you supply, for you), and why these sorts of methods > are covered in Django's documentation. I want a NullObject - not "create", which (correctly) implies a save() > In general, though, it appears you're trying to apply an anti-pattern: > silencing an exception which told you that something went wrong, and > then passing a "null" object into other code to try to compensate. > This is an approach more likely to create bugs than to solve them, and > so if implementing it is a bit harder than you'd like, perhaps that's > why. And again the condescension. As a programmer, I should be able to easily chose between statements that throw and ones that efficiently deal with branching conditions. A record-not-found is not a crisis, it is just a branching condition. 5 excess lines of code do not make my program more robust. Oh, look! "123"[:7] ! The string doesn't have 7 characters! Gee, shouldn't that throw an exception? -- Phlip -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: shortest way to recover from a QuerySet "object not found"
On Thu, Mar 4, 2010 at 12:59 PM, Phlipwrote: > Doesn't anyone in Django-land have experience with the platforms that > make this problem incredibly easy? Most likely, yes. And those people, believe it or not, designed Django's APIs based on their experiences. This is why there are shortcuts available, like the get_or_create() method (which fetches an object or, if none matches, creates a valid one, populated with default values you supply, for you), and why these sorts of methods are covered in Django's documentation. In general, though, it appears you're trying to apply an anti-pattern: silencing an exception which told you that something went wrong, and then passing a "null" object into other code to try to compensate. This is an approach more likely to create bugs than to solve them, and so if implementing it is a bit harder than you'd like, perhaps that's why. -- "Bureaucrat Conrad, you are technically correct -- the best kind of correct." -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: shortest way to recover from a QuerySet "object not found"
> from django.core.exceptions import ObjectDoesNotExist txbut... >sigh< I was hoping to head that off - Python's condescending attitude is in fact the core of the problem. Even if you wrap all your try: except: up in a method, so it's at least DRY, is you must consign that method to use .get(). So to squeeze my attempt into one line, we get foo = (list(Foo.objects.filter(name='bar')) + [Foo()])[0], which is way too much typing. Doesn't anyone in Django-land have experience with the platforms that make this problem incredibly easy? -- Phlip -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: shortest way to recover from a QuerySet "object not found"
On 4 March 2010 18:02, Phlipwrote: > Djangoids: > > Consider this line: > > foo = Foo.object.get(name='bar') > > If foo is not found, I want it to contain a NullObject, such as an > empty Foo(). In the parlance, that could be like this: > > foo = Foo.object.get(name='bar', _default=Foo()) > > I naturally don't expect (v 1.1.1) of Django to support my magic > _default keyword. > > What's the absolute shortest stretch of code which pops a NullObject > into my foo if the record 'bar' is not found? > > -- > Phlip > http://c2.com/cgi/wiki?ZeekLand > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > I'd say: from django.core.exceptions import ObjectDoesNotExist try: foo = Foo.object.get(name='bar') except ObjectDoesNotExist: foo = bar() but then again, there are all kinds of magic out there. :) -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
shortest way to recover from a QuerySet "object not found"
Djangoids: Consider this line: foo = Foo.object.get(name='bar') If foo is not found, I want it to contain a NullObject, such as an empty Foo(). In the parlance, that could be like this: foo = Foo.object.get(name='bar', _default=Foo()) I naturally don't expect (v 1.1.1) of Django to support my magic _default keyword. What's the absolute shortest stretch of code which pops a NullObject into my foo if the record 'bar' is not found? -- Phlip http://c2.com/cgi/wiki?ZeekLand -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.