I apologize for reviving this old question and being contrary, but I thought it might help for me to provide a full code snippet. I've mentioned get_or_create
a couple times but either you're misunderstanding me or vice versa. This code should be the same as what you wrote, except it uses Django's get_or_create
rather than reinventing it and needing to catch the IntegrityError
:
instance = Photo(title=form.data['title'])
instance.save()
tag, created = Tag.objects.get_or_create(name=form.data['tags'])
tag.photos.add(instance)
return HttpResponse("Succesful Upload with Tags")
Hope this helps. :-)