Alex Rivera | Logout

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

Asked 2010-06-15T17:57:00.380
11

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!

Edit
Report

1 Answer

2

If you don't want to update a field, remove it from the form via the Meta exclude tuple:

class Meta:
    exclude = ('title',)
answered 2010-06-15T18:17:40.823

Your Answer