2012-06-29 68 views
0

有关在models.py中正确配置related_name以创建类别和子类别结构的任何建议?运行syncdb时出错:Django related_name in Models

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' 

任何建议都非常赞赏。谢谢

回答

1

您不能同时调用类别和FK从子类别到类别“children”的递归关系。为其中一个选择一个不同的名称。

但是我必须说,这看起来不太适合MPTT。公司并不属于树木关系,因为它们大概都处于同一水平,苏打水也不属于这种类型:例如,“可乐”成为“波光粼粼的水”的孩子意味着什么?听起来你想要一个简单的从公司到SodaType的ManyToMany关系,这样每个公司都可以有许多种类型的汽水,每种类型都可以由几家公司制造。

+0

想要使用mptt,因为这种关系将比我上面提到的苏打/公司类比要深得多。非常感谢你的伟大建议。 –