Andrei Taranchenko Andrei Taranchenko - 1 year ago 179
Python Question

Django form fails validation on a unique field

I have a simple model that is defined as:

class Article(models.Model):
slug = models.SlugField(max_length=50, unique=True)
title = models.CharField(max_length=100, unique=False)

and the form:

class ArticleForm(ModelForm):
class Meta:
model = Article

The validation here fails when I try to update an existing row:

if request.method == 'POST':
form = ArticleForm(request.POST)

if form.is_valid(): # POOF

Creating a new entry is fine, however, when I try to update any of these fields, the validation no longer passes.

The "errors" property had nothing, but I dropped into the debugger and deep within the Django guts I saw this:

slug: "Article with this None already exists"

So it looks like is_valid() fails on a unique value check, but all I want to do is update the row.

I can't just do:

... because the form will fail on validation.

This looks like something very simple, but I just can't figure it out.

I am running Django 1.0.2

What croaks is BaseModelForm.validate_unique() which is called on form initialization.

Answer Source

I don't think you are actually updating an existing article, but instead creating a new one, presumably with more or less the same content, especially the slug, and thus you will get an error. It is a bit strange that you don't get better error reporting, but also I do not know what the rest of your view looks like.

What if you where to try something along these lines (I have included a bit more of a possible view function, change it to fit your needs); I haven't actually tested my code, so I am sure I've made at least one mistake, but you should at least get the general idea:

def article_update(request, id):
   article = get_objects_or_404(Article, pk=id)

   if request.method == 'POST':
      form = ArticleForm(request.POST, instance=article)

      if form.is_valid():

         return HttpResponseRedirect(to-some-suitable-url)

      form = ArticleForm(instance=article)

   return render_to_response('article_update.html', { 'form': form })

The thing is, as taurean noted, you should instantiate your model form with the object you wish to update, otherwise you will get a new one.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download