关于在 models.py 中正确配置related_name
以创建类别和子类别结构的任何建议?运行时出错syncdb
:
myapp.category: Accessor for field 'parent' clashes with related field 'Category.children'. Add a related_name argument to the definition for 'parent'.
myapp.category: Reverse query name for field 'parent' clashes with related field 'Category.children'. Add a related_name argument to the definition for 'parent'.
myapp.subcategory: Accessor for field 'parent' clashes with related field 'Category.children'. Add a related_name argument to the definition for 'parent'.
myapp.subcategory: Reverse query name for field 'parent' clashes with related field 'Category.children'. Add a related_name argument to the definition for 'parent'.
我正在尝试制作类别/子类别结构。例如,父类别是汽水公司(可口可乐、百事可乐等),子类别是汽水的类型(可乐、苏打水等)。子类别可以与不同的父类别相关联,反之亦然。
以下是我在弄清楚(使用django-mptt)时遇到问题的模型:
class Category(MPTTModel):
site = models.ForeignKey(Site)
template_prefix = models.CharField(max_length=200, blank=True)
name = models.CharField(max_length=200)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
def __unicode__(self):
return self.name + u' Category'
class SubCategory(MPTTModel):
name = models.CharField(max_length=50, unique=True)
parent = TreeForeignKey('Category', null=True, blank=True, related_name='children')
def __unicode__(self):
return self.name + u' SubCategory'
非常感谢任何建议。谢谢