Is there any documentation about how django handles different ORM problems/errors for methods:
- MyModel.objects.get(name=’myname’)
- myModel.save()
- myModel.delete()
I have read the documentation on https://docs.djangoproject.com/en/dev/ref/models/instances/ and find that some problems seem to raise exception, is this the case for all get, save, delete methods or are some of them just returning None or False. I cannot find any documentation that clearly states what kind of returns or exceptions to expect.
What is the coding conventions for this if I for example want to check if a username is free and don’t want my code to die if user does not exist?
user = User.objects.get(username='myusername')
if not user:
return True
return False
or
try:
user = User.objects.get(username='myusername')
except:
return True
return False
or
try:
user = User.objects.get(username='myusername')
except:
user = None
if not user:
return True
return False
And what values can save and delete return or do they always raise exceptions?
Take a look at django.core.exceptions.
https://docs.djangoproject.com/en/1.3/ref/exceptions/#module-django.core.exceptions