On Sat, Jan 24, 2009 at 12:52 PM, Rob Hudson <treborhud...@gmail.com> wrote:
> > I'm a bit at a loss as to where to go from here... > > I have simple Link and Category models. My Link model is essentially: > user_id, category (FK), name, URL. I have a Meta unique_together on > (user, url) so no duplicate URLs get entered. I'm testing this by > trying to purposefully enter a duplicate URL. > Just a note -- what you are describing is unique URLs per-user. You confused me a bit with how you phrased it as duplicate URLs are allowed by what you have specified, so long as they are associated with different users. > > At first my code threw an IntegrityError so I'm trying to catch that. > Here's my create view: > > def create(request): > > errors = {} > > if request.method == 'POST': > form = LinkForm(request, request.POST) > if form.is_valid(): > link = form.save(commit=False) > link.user = request.user You left out the form definition. I'm guessing it's a ModelForm? Also I'm guessing you excluded the user from the form? Note if it is a ModelForm, and all the fields involved in unique_together were in fact included in it, then the form validation would do the unique check for you, and you wouldn't have to worry about the save causing a database error, assuming you could guarantee no other save was made that might invalidate the unique check done by is_valid() before the committing save() ran. But if you exclude one of the fields or can't guarantee a duplicate entry hasn't been inserted between the validation check and the committing save, then you do need to deal with the potential database error. > try: > link.save() > except IntegrityError: > errors['Duplicate URL'] = 'That URL is already a > link.' > else: > return HttpResponseRedirect(reverse('links_index')) > else: > form = LinkForm(request) > > return render_to_response( > 'links/link_form.html', { > 'form': form, > 'errors': errors, > }, > context_instance=RequestContext(request) > ) > > But I get an InternalError. Trackback below: > > [snip] > Exception Type: InternalError at /links/create/ > Exception Value: current transaction is aborted, commands ignored > until end of transaction block > Anytime you catch a DB exception and try to proceed, at least under PostgreSQL, you'll see this error from whatever the next command sent to the DB is. > Since this looked like a transaction issue I tried adding a > transaction.rollback() to my IntegrityError except clause but that > threw its own error that it wasn't in transaction management. > So...if you can't call transaction.rollback() because the code is not under transaction management, ensure it is under transaction management whenever you might need to call transaction.rollback(). Probably easiest to just wrap it in commit_on_success: http://docs.djangoproject.com/en/dev/topics/db/transactions/#django-db-transaction-commit-on-success Alternatively you could use the lower-level connection rollback routine and bypass the django.db.transaction layer, but I think it's cleaner to stick with the higher level routines. Karen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---