4
我试图创建以下型号一个formset:Django的inlineformset_factory和多对多领域
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)
但我尝试实施一个formset,像这样的任何时间:
FormSet = inlineformset_factory(Category, Recipe, extra=3)
formset = FormSet()
我得到一个错误,说明类别模型中没有ForeignKey。是否可以使用ManyToManyField构建一个formset,或以某种方式复制此功能?
谢谢!
是啊,看起来像你说得对。我试图通过使用我自己的init和save方法创建自定义字段来解决这个问题。 – bento 2012-04-24 19:43:12
@bento,我知道这很老掉牙了,但是你有没有办法解决这个问题?我和你的情况一样,想知道你是如何解决它的。 – vleong 2015-09-16 14:11:11
很久很久以来我很久以前就使用过Django了,我真的不记得我最终做了什么。抱歉! – bento 2015-09-16 17:43:58