Inlinseformset does not save. and I don't understand

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

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

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

Views.py

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,)))

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

Thanks for answering so quicky and being efficient! :slight_smile: