相关疑难解决方法(0)

Django inlineformset_factory 和 ManyToMany 字段

我正在尝试为以下模型创建表单集:

class Category(models.Model):

    name = models.CharField(max_length=100, unique=True)
    description = models.TextField(null = True, blank=True)

class Recipe(models.Model):
    title = models.CharField(max_length=100)
    body = models.TextField()
    user = models.ForeignKey(User)
    categories = models.ManyToManyField(Category, null = True, blank = True)
Run Code Online (Sandbox Code Playgroud)

但每当我尝试实现表单集时,就像这样:

FormSet = inlineformset_factory(Category, Recipe, extra=3)
        formset = FormSet()
Run Code Online (Sandbox Code Playgroud)

我收到一条错误消息,指出类别模型中不存在外键。是否可以使用 ManyToManyField 构建表单集,或以某种方式复制此功能?

谢谢!

python django django-forms

6
推荐指数
1
解决办法
7506
查看次数

标签 统计

django ×1

django-forms ×1

python ×1