# Custom save() method in a form class

**URL:** <https://forum.djangoproject.com/t/custom-save-method-in-a-form-class/11594>\
**Category:** Django Internals\
**Created:** [January 14, 2022, 7:00pm UTC](https://forum.djangoproject.com/t/custom-save-method-in-a-form-class/11594 "2022-01-14T19:00:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![skyshy0707](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/skyshy0707/32/7079_2.png) [@skyshy0707](https://forum.djangoproject.com/u/skyshy0707)\
**Post date:** [January 14, 2022, 7:00pm UTC](https://forum.djangoproject.com/t/custom-save-method-in-a-form-class/11594/1 "2022-01-14T19:00:17Z")

</div>

Hello everyone! I have overrided method “save” in the form class to not lose existing related objects in ManyToManyField works of model Work and for good practice. But as you can see, 10th line in code of save() method does the absurd thing: blank value of name field is valid.

models.py:

```auto
class Work(models.Model):
 
    name = models.TextField(verbose_name='Name of work', max_length=1000)
    works = models.ManyToManyField('self', verbose_name='Work's objects', blank=True, default=list)
    select_type = models.CharField(verbose_name="Type of variant", max_length=11, choices=SelectTypes.choices, default=description('radio'))
 
    class Meta:
        verbose_name = 'Work'
        verbose_name_plural = 'Works'
 
    def __str__ (self,):
        return f"{self.name}"

```

forms.py:

```auto
class WorkForm(forms.ModelForm):
 
    class Meta:
        model = Work
        fields = ('name', 'works')
        labels = {'name': 'Название работы', 'works': 'Составные компоненты'}
 
    def save(self, commit=True):
  
        instance = super(WorkForm, self).save(commit=False)
 
        work_set = []
        if instance.id:
            work_set.extend(instance.works.all())
 
 
        if commit:
            self.save_m2m()
            instance.save()
            instance.works.add(*work_set)
        return instance

AddWorkComponents = modelformset_factory(
    Work,
    form=WorkForm,
    extra=1,
    can_delete=True,
    fields=('name',),
    labels={'name': 'Input a variant of the work'},
    widgets={'name': forms.TextInput(attrs={'class': 'form-control'})}
)

```

In html extra forms which aren’t have filled don’t have value attribute.:

`<input type="text" name="form-2-name" class="form-control" maxlength="1000" id="id_form-2-name">`

As i figure out this instruction:

`super(WorkForm, self)`

creates a new object with overrided attributes (but not methods) in the basic class for WorkForm. Attributes will have those values, which are in a derived class. But i don’t know that absurd with validation field.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 16, 2022, 3:49am UTC](https://forum.djangoproject.com/t/custom-save-method-in-a-form-class/11594/2 "2022-01-16T03:49:26Z")

</div>

Can you post your view here that is working with this Work model and WorkForm?
