# Inlinseformset does not save. and I don't understand

**URL:** <https://forum.djangoproject.com/t/inlinseformset-does-not-save-and-i-dont-understand/19693>\
**Category:** Forms & APIs\
**Created:** [March 21, 2023, 11:43am UTC](https://forum.djangoproject.com/t/inlinseformset-does-not-save-and-i-dont-understand/19693 "2023-03-21T11:43:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardWoj](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/richardwoj/32/12824_2.png) [@RichardWoj](https://forum.djangoproject.com/u/RichardWoj)\
**Post date:** [March 21, 2023, 11:43am UTC](https://forum.djangoproject.com/t/inlinseformset-does-not-save-and-i-dont-understand/19693/1 "2023-03-21T11:43:10Z")

</div>

Display is ok. But changes, and adding lines are not saved.  
When I changed a field in the data\_formset, it appears in request.POST, but the database is unchaged.  
When I add a new line to create a record, I get an error: "save() prohibited to prevent data loss due to unsaved related object ‘theme’.  
Has anyone an idea of what I’m doing wrong?

**models.py**

```auto
Display is ok. But changes, and adding lines are not saved.
**models.py**

class Theme(models.Model):
    label = models.CharField(max_length=20)
    suite = models.CharField(max_length=20, null=True, blank=True)

    class Meta:
        ordering = ['label']

    def __str__ (self):
        return self.label
   
class Data(models.Model):
    theme =models.ForeignKey(Theme, on_delete=models.CASCADE, null=True, blank=True,default = 1)
    champ1 = models.CharField(max_length=20)
    champ2 = models.CharField(max_length=20)

    class Meta:
        ordering = ['champ1']

    def __str__ (self):
        return self.champ1

```

**forms.py**

```auto
class DataForm(ModelForm):
    class Meta:
        model = Data
        fields = ' __all__'
        
class ThemeForm(ModelForm):
    class Meta:
        model = Theme
        fields = ' __all__'  

```

**Views.py**

```auto
class ThemeUpdateView(UpdateView):
    template_name = 'gestcom/theme_detail_form.html'
    model = Theme
    fields = ' __all__'
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        theme = get_object_or_404(Theme, pk=self.kwargs['pk'])  
        context['theme_form'] = ThemeForm(instance = theme)
        DataFormSet = inlineformset_factory(Theme,Data, can_delete=True, form = DataForm)
        data_formset = DataFormSet(instance=theme)
        context['data_formset'] = data_formset
        return context
    def post(self, request, *args, **kwargs):
        # récupération de la clé primaire depuis l'url
        pk = self.kwargs['pk'] 
        # récupération de l'enregistrement Theme à partir de pk
        theme = get_object_or_404(Theme, pk=pk)      
        # création des forms
        theme_form = ThemeForm(instance=theme, data=request.POST)
        DataFormSet = inlineformset_factory(Theme, Data, can_delete=True, form = DataForm)
        data_formset = DataFormSet(request.POST)
        if 'update' in request.POST:
            if theme_form.is_valid() and data_formset.is_valid():
                messages.success(request,'saved')
                messages.success(request,request.POST)
                theme_form.save()
                data_formset.save()
            else:
                messages.warning(request, 'formulaire invalide')
                messages.warning(request, 'Thème:' + theme_form.errors)
                messages.warning(request, 'Data:' + data_formset.errors)
                messages.warning(request, request.POST)
        return HttpResponseRedirect(reverse('theme_detail_form', args=(theme.id,)))

```

---

<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:** [March 21, 2023, 12:00pm UTC](https://forum.djangoproject.com/t/inlinseformset-does-not-save-and-i-dont-understand/19693/2 "2023-03-21T12:00:25Z")

</div>

I believe you want to pass the `instance` parameter in the `DataFormSet` call inside the post along with the `request.POST` data.

---

<div class="post-metadata">

**Author:** ![RichardWoj](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/richardwoj/32/12824_2.png) [@RichardWoj](https://forum.djangoproject.com/u/RichardWoj)\
**Post date:** [March 21, 2023, 12:36pm UTC](https://forum.djangoproject.com/t/inlinseformset-does-not-save-and-i-dont-understand/19693/3 "2023-03-21T12:36:43Z")

</div>

Thanks for answering so quicky and being efficient! 🙂
