我想创建嵌套的类别,模型运行良好。
class Category(models.Model):
category_name = models.CharField(max_length=100)
children_ids = models.ManyToManyField(
"Category", blank=True, related_name="categories"
)
...etc
但是,当我为管理面板添加内联时
class ChildrensInline(admin.TabularInline):
model = Category.children_ids.through
编译器显示错误:"categories.Category_children_ids"有多个指向"Category.Category"的ForeignKey。必须指定"fk_name"特性。
我还尝试了fk_name="类别",以及Category_children_ids表中的列名,但它不适用于
fk_name
的值必须是类的名称(小写(,前缀为"from_"
或"to_"
,具体取决于您的需要。因此,在您的情况下,它必须是fk_name='from_category'
或fk_name='to_category'
:
class ChildrensInline(admin.TabularInline):
model = Category.children_ids.through
fk_name='from_category' # or: 'to_category'
一般来说,解决这些问题的一个快速方法是(暂时(在代码中的任何位置放置一个简单的print(model.__dict__)
,例如在第二行之后。然后model
的所有字段将显示在控制台输出中。