What's the best way to handle Django's objects.get?

Whenever I do this:

thepost = Content.objects.get(name="test")

It always throws an error when nothing is found. How do I handle it?

Answers


from django.core.exceptions import ObjectDoesNotExist

try:
    thepost = Content.objects.get(name="test")
except ObjectDoesNotExist:
    thepost = None

I'm writing this from memory, so I'm not sure about the exception.


Need Your Help

Castor XML Mapping same name with the child node

java xml-parsing castor

I am facing a problem with castor while trying to parse an xml which contains same name for the parent and child node... Here are the details: