# inlineformset not saving to datebase

**URL:** <https://forum.djangoproject.com/t/inlineformset-not-saving-to-datebase/29346>\
**Category:** Forms & APIs\
**Created:** [March 21, 2024, 4:49pm UTC](https://forum.djangoproject.com/t/inlineformset-not-saving-to-datebase/29346 "2024-03-21T16:49:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SimonJones](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/simonjones/32/20071_2.png) [@SimonJones](https://forum.djangoproject.com/u/SimonJones)\
**Post date:** [March 21, 2024, 4:49pm UTC](https://forum.djangoproject.com/t/inlineformset-not-saving-to-datebase/29346/1 "2024-03-21T16:49:02Z")

</div>

Hi

I’m new to django and have been building upon the polls application for a personal project.  
This issue that I have, as the title suggests, is that my use of InlineFormSet within admin isn’t saving to the database. I feel like it is my solution for having a pre-populated TubularInLine fields within my polls choice model so that I don’t have to manually input 2 choices into my poll every time a poll is created. It would seem that the use of InlineFormSet will allow me to have 2 constant options in these fields.

The goal is that the user is able to enter a date, hit submit and then the poll is generated with the choices being automatically set to ‘yay’ / ‘nay’

When I’m creating the poll via the form in my application, everything is working at is should with the poll being generated client-side and within admin.  
Within admin, the fields are being populated with ‘Yay’ and ‘Nay’ as desired, however, they aren’t being saved to the database and therefor, not being rendered as possible selections within my poll.

Please forgive me if any of this is confusing…  
Any help with this would be massively appreciated. Thank you.

models.py

```auto
class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200, null=True, blank=True)
    votes = models.IntegerField(default=0, null=True, blank=True)

    def __str__ (self):
        return self.choice_text

```

admin.py

```auto
class ChoiceInLineFormset(BaseInlineFormSet):
    def __init__ (self, *args, **kwargs):
        kwargs['initial'] = [
            {'choice_text': 'Yay'}, {'choice_text': 'Nay'}
        ]
        super(ChoiceInLineFormset, self). __init__ (*args, **kwargs)

class ChoiceInline(admin.TabularInline):
    model = Choice
    extra = 2
    max_num = 2
    formset = ChoiceInLineFormset

```

views.py

```auto

@login_required(login_url="/")
def createPoll(request, *args, **kwargs):
    form = QuestionForm()

    if request.method == 'POST':
        form = QuestionForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/polls/')

    context = {'form': form}
    return render(request, 'polls/poll-form.html', context)

```

forms.py

```auto
class QuestionForm(ModelForm):
    class Meta:
        model = Question
        fields = ['question_text']

        widgets = {
            'question_text': forms.TextInput(attrs={'placeholder': 'Date: xx/xx/xxxx'}),
        }

```

Thanks

---

<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, 2024, 5:58pm UTC](https://forum.djangoproject.com/t/inlineformset-not-saving-to-datebase/29346/2 "2024-03-21T17:58:57Z")

</div>

The issue is that if you are passing the initial value to the forms when the forms in the formset are being bound, then Django doesn’t detect this as data being submitted.

You would need to initialize the forms with the data on the “GET”, but not on the “POST”.

(No, I don’t know what that would look like in terms of doing this in the admin.)

Or, instead of doing it _here_, you could override the `save_related` method on the ModelAdmin class and do it there.

---

<div class="post-metadata">

**Author:** ![SimonJones](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/simonjones/32/20071_2.png) [@SimonJones](https://forum.djangoproject.com/u/SimonJones)\
**Post date:** [March 22, 2024, 7:41am UTC](https://forum.djangoproject.com/t/inlineformset-not-saving-to-datebase/29346/3 "2024-03-22T07:41:42Z")

</div>

Ken, thank you for taking the time to take a look and explain some solutions to explore. I shall take a look today and report back with the solution.

---

<div class="post-metadata">

**Author:** ![Douglasemmanuel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/douglasemmanuel/32/20084_2.png) [@Douglasemmanuel](https://forum.djangoproject.com/u/Douglasemmanuel)\
**Post date:** [March 22, 2024, 5:58pm UTC](https://forum.djangoproject.com/t/inlineformset-not-saving-to-datebase/29346/4 "2024-03-22T17:58:48Z")

</div>

Please could anyone help me out , I got 500 servers less function crashed after deploying Django on vercel

---

<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 22, 2024, 6:01pm UTC](https://forum.djangoproject.com/t/inlineformset-not-saving-to-datebase/29346/5 "2024-03-22T18:01:01Z")

</div>

Welcome @Douglasemmanuel !

If you’re looking for assistance with a specific issue, please open a new topic for that issue along with the details of what you think may be causing it.
