[Django]-Django form fails validation on a unique field

28👍

✅

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():
         form.save()

         return HttpResponseRedirect(to-some-suitable-url)

   else:
      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.

5👍

I was also searching for a way to update an existing record, even tried form.save(force_update=True) but received errors??
Finally by trial & error managed to update existing record. Below codes tested working. Hope this helps…

models.py from djangobook

class Author(models.Model):
    first_name = models.CharField(max_length=30)

    last_name = models.CharField(max_length=40)

    email = models.EmailField(blank=True, verbose_name='e-mail')

    objects = models.Manager()

    sel_objects=AuthorManager()

    def __unicode__(self):
        return self.first_name+' '+ self.last_name

class AuthorForm(ModelForm):
    class Meta:
        model = Author


# views.py
# add new record

def authorcontact(request):

    if request.method == 'POST':

        form = AuthorForm(request.POST)

        if form.is_valid():

            form.save()

            return HttpResponseRedirect('/contact/created')

    else:

        form = AuthorForm()

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

update existing record

def authorcontactupd(request,id):

    if request.method == 'POST':

        a=Author.objects.get(pk=int(id))

        form = AuthorForm(request.POST, instance=a)

        if form.is_valid():

            form.save()

            return HttpResponseRedirect('/contact/created')

    else:
        a=Author.objects.get(pk=int(id))

        form = AuthorForm(instance=a)

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

2👍

All i can guess is that you are getting an object to fill a form, and trying to save it again.

Try using a ModelForm, and intantiate it with desired object.

1👍

It appears that your SlugField is returning None and because a null/blank slug already exists somewhere in the database, its giving an ‘already exists’ error. It seems like your slug field isn’t saving correctly at all.

Leave a comment