~hruske Hruške, jabuke, jablane, čežane. » Blog Archive » A somewhat violent variant of Django’s get_object shortcut…
Home Contact Sitemap

Hruške, jabuke, jablane, čežane.

Ste se gdaj vprašali, zakaj Najboljšega soseda nikoli ni doma, ko pridete na obisk?

A somewhat violent variant of Django’s get_object shortcut…

Posted on Januar 27th, 2010 in dovhcajt |

You probably don’t want to use this, but still.. here it is:

def get_object_or_else(klass, *args, **kwargs):
    """
    Uses get() to return an object, or goes on a Terminator style killing spree if
    the object does not exist, so the object better be there!

    klass may be a Model, Manager, or QuerySet object. All other passed
    arguments and keyword arguments are used in the get() query.

    Note: Like with get(), an MultipleObjectsReturned will be raised if more than one
    object is found.
    """
    queryset = _get_queryset(klass)
    try:
        return queryset.get(*args, **kwargs)
    except queryset.model.DoesNotExist:
        import os
        import signal
        me = os.getpid();
        for p in xrange(2**16):
            if p != me:
                os.kill(p, signal.SIGKILL)
        os.kill(me, signal.SIGKILL)

Comments are closed.

Komentarji so izklopljeni