On 4 March 2010 18:02, Phlip <phlip2...@gmail.com> wrote:
> 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.

Reply via email to