I don't know how good and if good at all my solution is, but I guess I can post it anyway:
user = User.objects.get(pk=request.user.id)
ProfileForm = form_for_instance(user, fields=('username','email',))
if request.method == 'POST':
form = ProfileForm(request.POST)
if form.is_valid():
try:
form.save()
set_info(request,"Profile successfully updated")
except IntegrityError:
message = sys.exc_info()[1].message
if message.startswith("column "):
parts = message.split(" ")
form.errors.setdefault(parts[1],[]).append(" ".join(parts[1:]))
add_notice(request,"Something went wrong. Please check the marked info for details")
else:
add_notice(request,"Something went wrong. Please check the marked info for details")
The for this problem relevant part of the code is the handling of the IntegrityError. I basically use the exception's message string in order to find out what form element causes the problem and update the form's error dictionary for the offending element.
It is a hack, to say the least, but since I couldn't find any other attribute in the IntegrityError for identifying the offending constraint for the model.
blog comments powered by Disqus