title: How to create an inline formset with manytomany relation in Django
body: <p>I want to create an inline formset between <strong>Preorder</strong> model and <strong>Product</strong> model. The scenario is that the user will be able to select one or more than one products when he decides to create a preorder. On the other hand a product might be found in one or more than one preorders. With that in mind i created a manytomany relationship.</p>
  
  <p>models.py</p>
  
  <pre><code>class Preorder(models.Model):
  
      client = models.ForeignKey(Client,verbose_name=u'Client')
      invoice_date = models.DateField("Invoice date",null=True, blank=True, default=datetime.date.today)
      preorder_has_products = models.ManyToManyField(Product, blank=True)
  
      def get_absolute_url(self):
          return reverse('preorder_edit', kwargs={'pk': self.pk})
  
  
  class Product(models.Model):
  
      name = models.CharField("Name",max_length=200)
      price = models.DecimalField("Price", max_digits=7, decimal_places=2, default=0)
      barcode = models.CharField(max_length=16, blank=True, default="")
      eopyy = models.CharField("Code eoppy",max_length=10, blank=True, default="")
      fpa = models.ForeignKey(FPA, null=True, blank=True, verbose_name=u'Fpa Scale')
  </code></pre>
  
  <p>forms.py</p>
  
  <pre><code>class PreorderForm(ModelForm):
  
      class Meta:
          model = Preorder
          exclude = ('client','preorder_has_products',)
  
      def __init__(self, *args, **kwargs):
          super(PreorderForm, self).__init__(*args,**kwargs)
          self.fields['invoice_date'].widget = MyDateInput(attrs={'class':'date'})
  
  class ProductForm(ModelForm):
      #name = ModelChoiceField(required=True,queryset=Product.objects.all(),widget=autocomplete.ModelSelect2(url='name-autocomplete'))
      class Meta:
          model=Product
          fields = '__all__'
  
      def __init__(self, *args, **kwargs):
          super(ProductForm, self).__init__(*args, **kwargs)
          self.fields['name'].label="Name"
          self.fields['price'].label="Price"
  </code></pre>
  
  <p>and finally the inline formset:</p>
  
  <pre><code>PreorderProductFormSet = inlineformset_factory(Preorder, Product,
                                               form=ProductForm, extra=1)
  </code></pre>
  
  <p>After run I face up the issue:<strong>ValueError at /
  'intranet.Product' has no ForeignKey to 'intranet.Preorder'</strong></p>
  
  <p>Why this happening since I created a manytomany relation?</p>
  
  <p>One solution is to create a foreign key relationship between Preorder and Product model inside Product model..but I do not want to do that since product model is used in other areas of my project and do not want to mess it up.</p>
  
  <p>Any suggestions?</p>