title: How to filter the queryset of a django modelformset manytomany memeber
body: <p>I can't use the proper code but I found the pizza/toppings question and it's close so I'm modifying it to ask my question.  <a href="https://stackoverflow.com/questions/2216974/django-modelform-for-many-to-many-fields">Django ModelForm for Many-to-Many fields</a></p>
  
  <p>We have Pizza and Topping which are great.  And let's say that we operate a chain of stores and not all stores have all ingredients.  Which means we need a Store class that has a location and a manytomany for toppings.  And then let's roll an Order which references a store and has a manytomany on Pizza since you often order more than one.</p>
  
  <p>models.py</p>
  
  <pre><code>from django import models
  
  class Topping(models.Model):
      name = models.TextField()
  
  class Pizza(models.Model):
      size = models.TextField()
      toppings = models.ManyToManyField(Topping)
  
  class Restaraunt(models.Model):
      name = models.TextField()
      toppings = models.ManyToManyField(Topping)
  
  
  class Order(models.Model):
      customer = models.ForeignKey('User')
      location = models.ForeignKey(Restaraunt)
      pizzas = models.ManyToManyField(Pizza)
  </code></pre>
  
  <p>forms.py</p>
  
  <pre><code>from django import forms
  from django.forms import ModelForm
  from models import *
  
  class PizzaForm(ModelForm):
  
      class Meta:
          model = Pizza
  
      toppings = forms.ModelMultipleChoiceField(
          widget=forms.CheckboxSelectMultiple(),
          queryset=Topping.objects.all()
      )
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>from django.shortcuts import render, redirect, get_object_or_404
  from django.forms.models import modelformset_factory
  from django.forms import CheckboxSelectMultiple
  
  from models import *
  from forms import *
  
  
  def order_pizzas(request, order_id):
      order_id = int(order_id)
      order = get_object_or_404(Order, id=order_id)
  
      restaraunt = order.restaraunt
  
      PizzaFormSet = modelformset_factory(Pizza, form=PizzaForm, extra=1)
  
      pizza_form = PizzaFormSet(request.POST or None)
  
      return render(request, "place_order.html", {
          'restaraunt': restaraunt,
          'pizza_form': pizza_form,
      })
  </code></pre>
  
  <p>Please don't beat me up too much if those don't actually run.  Like I said I can't post the real code here for various reasons, partially because it's just massive.</p>
  
  <p>For the sake of this example assume that the person has already located the restaraunt closest to their home and started the ordering process.</p>
  
  <p>I have tried passing in a widget to the modelformset_factory with the proper name and options but that didn't propagate.</p>
  
  <pre><code>widgets = {'toppings': 
      CheckboxSelectMultiple(
          choices=[t.id for t in restaraunt.toppings.all().order_by('-id')]
      )}
  </code></pre>
  
  <p>I also tried extending the BaseModelFormSet to pass in extra data and try and get that into the PizzaForm but I got stuck.</p>
  
  <p>Basically I can tell that I need to somehow propagate information from the view to the formset to the form so that the queryset on the form can be initialized properly.  I just can't figure out precisely how to do that.</p>
  
  <p>So this is the closest I've found to an answer but I can't seem to figure out how to make that work for sure:
  <a href="https://stackoverflow.com/questions/7371392/django-filter-modelformset-field-choices-different-from-limiting-the-formset">Django filter ModelFormSet field choices... different from limiting the Formset&#39;s queryset</a></p>
  
  <h1>In response to people who are going to badger me for posting a non-working example</h1>
  
  <p>The point is that I'm not trying to ask for someone to post the exact 10 lines of code that'll completely solve my problem, but rather that I'm showing where my knowledge is lacking.  I know that I generate a PizzaFormSet and that eventually somewhere in the bowels of that code it uses the PizzaForm I specify.  But I don't have any idea how to successfully pass information from the PizzaFormSet to the PizzaForm.</p>
  
  <p>Basically I'm willing to give away a bounty for a suggestion as to what part of this puzzle I'm missing.</p>
  
  <h1>Where the problem is</h1>
  
  <p>I have defined a form in forms.py (PizzaForm) which needs to get a situationally-dependent queryset for the Topping.  The view order_pizzas determines which restaurant will make and deliver the pizzas and the toppings available at that restaurant might be different than at other restaurants.</p>
  
  <p>I don't know how to propagate that information from the view to the form, normally you just subclass the form and add some extra init kwargs to do whatever you want.</p>
  
  <p>But in this case I'm using a formset rather than a single form.  That means I have to find (or make) some channel to pass the restaurant information and/or the specific queryset through from the view to the formset to the form.  I think that's my main point of confusion and/or ignorance.</p>