title: How do I use django formset_factories?
body: <p>I am trying to work on a django-based project/website that logs who took out our dogs last. I am very new to django so please bear with lack of knowledge in the field. I currently have my project set up so that I have models based on users/family members, the families' dogs, posts (logs of when and who took out the dogs), and actions, which is a child class that uses the dog and post models as foreign keys to see if the dogs peed and or pooped when they were taken out. The part that I am stuck on is trying to figure out how I create the post form. Since we have two dogs I need the form/post page to display a set of check boxes (for peeing and pooping actions) for each dog model that exists. While I can successfully display one action set, I run into the difficulty of trying to display the correct number of action sets and also to post the set of actions and the post itself. I have been trying to work with the formset_factory function but I am confused as to how I get it to function properly. Can anyone help? I have been stuck on this problem for quite a while and any support would be greatly appreciated.</p>
  <p>models.py:</p>
  <pre><code>from django.db import models
  from django.utils import timezone
  from django.contrib.auth.models import User
  from django.urls import reverse
  
  class Dog(models.Model):
      name = models.CharField(max_length = 20)
  
  class Post(models.Model):
      walker = models.ForeignKey(User, on_delete = models.CASCADE)
      time_posted = models.DateTimeField(default = timezone.now)
      issues = models.TextField(max_length = 300)
  
  class Action(models.Model):
      post = models.ForeignKey(Post, on_delete = models.CASCADE)
      dog = models.ForeignKey(Dog, on_delete = models.CASCADE)
      peed = models.BooleanField(default = False)
      pooped = models.BooleanField(default = False)
  </code></pre>
  <p>forms.py:</p>
  <pre><code>from django import forms
  from django.forms import formset_factory
  from dog_log_app.models import *
  
  class Log_Form(forms.ModelForm):  
      class Meta:  
          model = Post
          fields = (&quot;issues&quot;,)
  
  class Action_Form(forms.Form):
      peed = forms.BooleanField(initial = False, required = False)
      pooped = forms.BooleanField(initial = False, required = False)
  </code></pre>
  <p>views.py:</p>
  <pre><code>from django.shortcuts import render
  from django.forms import formset_factory, modelformset_factory
  from .models import Post, Dog, Action
  from dog_log_app.forms import *
  from django.contrib.auth.models import User
  from django.http import HttpResponse, HttpResponseRedirect
  
  def home(request):
      context = {
          'posts': Post.objects.all().order_by('-time_posted'),
          'actions': Action.objects.all(),
          'dogs': Dog.objects.all()
      }
      return render(request, 'dog_log_app/home.html', context)
  
  def post(request):
      form_post = Log_Form(request.POST or None)
      form_actions = modelformset_factory(Action, fields = ('peed', 'pooped'), extra = Dog.objects.count())
  
      if request.method == 'POST':
          form_post = Log_Form(request.POST)
  
  
          if form_post.is_valid() and form_actions.is_valid():
              post_save = form_post.save(commit = False)
              post_save.walker = request.user
              post_save.save()
              form_actions.save()
  
              return HttpResponseRedirect('..')
      
      context = {
          'post': form_post,
          'action': formset_factory(Action_Form, extra = Dog.objects.count()),
          'dogs': Dog.objects.all()
      }
      return render(request, 'dog_log_app/post_form.html', context)
  </code></pre>
  <p>post_form.html:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
      &lt;meta charset=&quot;UTF-8&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
      &lt;title&gt;Post&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div&gt;
          &lt;form method=&quot;POST&quot;&gt;
              {% csrf_token %}
              &lt;fieldset&gt;
                  &lt;legend&gt;Create Post&lt;/legend&gt;
                  &lt;h3&gt;{{dogs.name}}&lt;/h3&gt;
                  &lt;p&gt;{{action.as_p}}&lt;/p&gt;
                  &lt;p&gt;{{post.as_p}}&lt;/p&gt;
              &lt;/fieldset&gt;
              &lt;div&gt;
                  &lt;button type=&quot;submit&quot; value=&quot;Ok&quot;&gt;Post&lt;/button&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>