How can I update only certain fields in a Django model form?

Posted by J. Frankenstein on Stack Overflow See other posts from Stack Overflow or by J. Frankenstein
Published on 2010-06-15T17:57:00Z Indexed on 2010/06/15 18:32 UTC
Read the original article Hit count: 190

Filed under:
|

I have a model form that I use to update a model.

class Turtle(models.Model):
    name = models.CharField(max_length=50, blank=False)
    description = models.TextField(blank=True)

class TurtleForm(forms.ModelForm):
    class Meta:
        model = Turtle

Sometimes I don't need to update the entire model, but only want to update one of the fields. So when I POST the form only has information for the description. When I do that the model never saves because it thinks that the name is being blanked out while my intent is that the name not change and just be used from the model.

    turtle_form = TurtleForm(request.POST, instance=object)
    if turtle_form.is_valid():
        turtle_form.save()

Is there any way to make this happen? Thanks!

© Stack Overflow or respective owner

Related posts about python

Related posts about django