Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

django

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!

like image 596
J. Frankenstein Avatar asked Jun 15 '10 17:06

J. Frankenstein


People also ask

How do I update model fields in Django?

Use update_fields in save() If you would like to explicitly mention only those columns that you want to be updated, you can do so using the update_fields parameter while calling the save() method. You can also choose to update multiple columns by passing more field names in the update_fields list.

How do I make a field optional in Django?

You would have to add blank=True as well in field definition. If the model field has blank=True, then required is set to False on the form field. Otherwise, required=True. Don't forget to reset and sync DB again after changing this.

How do you override a field widget in form for model?

If you want to override the widget for a formfield in general, the best way is to set the widgets attribute of the ModelForm Meta class: To specify a custom widget for a field, use the widgets attribute of the inner Meta class. This should be a dictionary mapping field names to widget classes or instances.

What is Is_valid () in Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.


2 Answers

Only use specified fields:

class FirstModelForm(forms.ModelForm):
    class Meta:
        model = TheModel
        fields = ('title',)
    def clean_title(self....

See http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#controlling-which-fields-are-used-with-fields-and-exclude

It is common to use different ModelForms for a model in different views, when you need different features. So creating another form for the model that uses the same behaviour (say clean_<fieldname> methods etc.) use:

class SecondModelForm(FirstModelForm):
    class Meta:
        model = TheModel
        fields = ('title', 'description')
like image 198
mawimawi Avatar answered Sep 28 '22 04:09

mawimawi


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

class Meta:
    exclude = ('title',)
like image 44
Daniel Roseman Avatar answered Sep 28 '22 04:09

Daniel Roseman