how to catch the MultipleObjectsReturned error in django
Use a filter:
Location.objects.filter(name='Paul').first()
Or import the exception:
from django.core.exceptions import MultipleObjectsReturned
...
try:
Location.objects.get(name='Paul')
except MultipleObjectsReturned:
Location.objects.filter(name='Paul').first()
This is more pythonic way to do it.
try:
Location.objects.get(name='Paul')
except Location.MultipleObjectsReturned:
Location.objects.filter(name='Paul')[0]