# Adding an InlineForm to the TabularInline results in multiple empty objects shown

**URL:** <https://forum.djangoproject.com/t/adding-an-inlineform-to-the-tabularinline-results-in-multiple-empty-objects-shown/35119>\
**Category:** The Admin\
**Created:** [September 26, 2024, 4:15am UTC](https://forum.djangoproject.com/t/adding-an-inlineform-to-the-tabularinline-results-in-multiple-empty-objects-shown/35119 "2024-09-26T04:15:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![xtlc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/xtlc/32/3887_2.png) [@xtlc](https://forum.djangoproject.com/u/xtlc)\
**Post date:** [September 26, 2024, 4:15am UTC](https://forum.djangoproject.com/t/adding-an-inlineform-to-the-tabularinline-results-in-multiple-empty-objects-shown/35119/1 "2024-09-26T04:15:09Z")

</div>

These are my models:

```auto
class Template(models.Model):
    class Pages(models.IntegerChoices):
        COVER = 0, "COVER"
        FIRST = 1, "FIRST"
        AFTERFIRST = 2, "BEFORETABLE"
        BEFORELAST = 3, "AFTERTABLE"
        LAST = 4, "LAST"
    name = models.CharField("name", max_length=128)
    page = models.PositiveIntegerField(choices=Pages.choices) 
    text = models.TextField("html allowed", blank=True, null=True)

class Inquiry(models.Model):
    name = models.CharField(max_length=64)
    templates = models.ManyToManyField(Template, through="InquiryTemplate")

class InquiryTemplate(models.Model):
    inquiry = models.ForeignKey(Inquiry, on_delete=models.CASCADE)
    template = models.ForeignKey(Template, on_delete=models.CASCADE)

    class Meta: 
        unique_together = ("inquiry", "template")

    def __str__ (self):
        return f"{self.template.get_page_display()}"

```

and this is the admin:

```auto

class InquiryTemplateInlineForm(forms.ModelForm):
    cover_template = forms.ModelChoiceField(queryset=Template.objects.filter(page=Template.Pages.COVER), required=False, label="Cover Template")
    first_template = forms.ModelChoiceField(queryset=Template.objects.filter(page=Template.Pages.FIRST), required=False, label="First Template")
    beforetable_template = forms.ModelChoiceField(queryset=Template.objects.filter(page=Template.Pages.BEFORELAST), required=False, label="Before Table Template")
    aftertable_template = forms.ModelChoiceField(queryset=Template.objects.filter(page=Template.Pages.AFTERFIRST), required=False, label="After Table Template")
    last_template = forms.ModelChoiceField(queryset=Template.objects.filter(page=Template.Pages.LAST), required=False, label="Last Template")

    class Meta:
        model = InquiryTemplate
        fields = ["cover_template", "first_template", "beforetable_template", "aftertable_template", "last_template"]

    def __init__ (self, *args, **kwargs):
        inquiry = kwargs.pop("inquiry", None)
        super(). __init__ (*args, **kwargs)
        if inquiry:
            existing_templates = InquiryTemplate.objects.filter(inquiry=inquiry)
            for template in existing_templates:
                if template.template.page == Template.Pages.COVER:
                    self.initial["cover_template"] = template.template
                elif template.template.page == Template.Pages.FIRST:
                    self.initial["first_template"] = template.template
                elif template.template.page == Template.Pages.BEFORELAST:
                    self.initial["beforetable_template"] = template.template
                elif template.template.page == Template.Pages.AFTERFIRST:
                    self.initial["aftertable_template"] = template.template
                elif template.template.page == Template.Pages.LAST:
                    self.initial["last_template"] = template.template

class InquiryTemplateInline(admin.TabularInline):
    model = InquiryTemplate
    form = InquiryTemplateInlineForm
    extra = 0  

    def get_formset(self, request, obj=None, **kwargs):
        formset = super().get_formset(request, obj, **kwargs)
        formset.inquiry = obj  
        return formset

class InquiryAdmin(admin.ModelAdmin):
    inlines = [InquiryPartInline, InquiryTemplateInline] 

    def save_formset(self, request, form, formset, change):
        super().save_formset(request, form, formset, change)
        InquiryTemplate.objects.filter(inquiry=form.instance).delete()
        if formset.cleaned_data[0].get("cover_template"):
            InquiryTemplate.objects.create(inquiry=form.instance, template=cover_template)
        if formset.cleaned_data[0].get("first_template"):
            InquiryTemplate.objects.create(inquiry=form.instance, template=first_template)
        if formset.cleaned_data[0].get("beforetable_template"):
            InquiryTemplate.objects.create(inquiry=form.instance, template=beforetable_template)
        if formset.cleaned_data[0].get("aftertable_template"):
            InquiryTemplate.objects.create(inquiry=form.instance, template=aftertable_template)
        if formset.cleaned_data[0].get("last_template"):
            InquiryTemplate.objects.create(inquiry=form.instance, template=last_template)

```

This looks like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/8/d/8d1a119787c7e2e546ca7fb6c0922bc9554ed100.png)

which is exactly what I want from the functional point of view. But:

- I thought when I override the `get_formset()` method of the `TabularInline` I could use my `Inquiry` instance in the `InquiryTemplateInlineForm` to prepopulate it with values, but `inquiry` is `None` here
- When i set for example 4 templates and click save → I get 4 rows of templates on reload.
