Django inlineformset_factory and ManyToMany fields

前端 未结 1 681
一个人的身影
一个人的身影 2020-12-21 02:43

I\'m attempting to create a formset for the following models:

class Category(models.Model):

    name = models.CharField(max_length=100, unique=True)
    des         


        
相关标签:
1条回答
  • 2020-12-21 03:11

    According to source code and documentation its only for foreign keys

    So if you want create a formset for your models you have to change

    categories = models.ManyToManyField(Category, null = True, blank = True)
    

    to

    categories = models.ForeignKey("Category", null = True, blank = True)
    

    Documentation: https://docs.djangoproject.com/en/1.4/topics/forms/modelforms/#inline-formsets https://docs.djangoproject.com/en/1.4/topics/forms/modelforms/#more-than-one-foreign-key-to-the-same-model

    Django source:

    def inlineformset_factory(parent_model, model, form=ModelForm,
                              formset=BaseInlineFormSet, fk_name=None,
                              fields=None, exclude=None,
                              extra=3, can_order=False, can_delete=True, max_num=None,
                              formfield_callback=None):
        """
        Returns an ``InlineFormSet`` for the given kwargs.
    
        You must provide ``fk_name`` if ``model`` has more than one ``ForeignKey``
        to ``parent_model``.
        """
    
    0 讨论(0)
提交回复
热议问题