2011-03-03 72 views
0

请看看:Django的继承

class Categorie(models.Model): 
    id = models.AutoField('id', primary_key=True) 
    title = models.CharField('title', max_length=800) 
    articles = models.ManyToManyField(Article) 

class Article(models.Model): 
    id = models.AutoField('id', primary_key=True) 
     title = models.CharField('title', max_length=800) 
    slug = models.SlugField() 
    indexPosition = models.IntegerField('indexPosition', unique=True) 

class CookRecette(Article): 
    ingredient = models.CharField('ingredient', max_length=100) 

class NewsPaper(Article): 
    txt = models.CharField('ingredient', max_length=100) 

因此,我创建 “CookRecette” 和 “报纸” 的 “文章”。 我也创建一个链接到(manyToMany)“Article”的“Categorie”类。

但是在管理界面中,我无法从“Categorie”链接到“CookRecette”或“NewsPaper”。 同样来自代码。 有什么帮助吗?

干杯,
马丁Magakian

PS:我很抱歉,但实际上该代码是正确的!所以,一切工作正常,我可以看到我的“CookRecette”或“报纸”,从“Categorie”

回答

1

我会说,你不需要定义“ID”字段,如果你开始不要定义它,然后Django会自动添加它。其次,CookRecette和NewsPaper对象并没有通过任何方式(ForeignKey,OneToOne,OneToMany,ManyToMany)链接到Categorie对象,因此无论如何都无法以这种方式访问​​Cookrecette和NewsPaper对象。

将模型以任何希望的方式链接在一起后,您可能想看看http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin,它将向您展示如何快速编辑Djano管理控制台中的相关对象。

0

NewsPaper有它的一部分作为Article对象。如果您将创建新的NewsPaper对象,则会在文章中看到新对象。因此,在管理界面中,管理类别时,您可以选择任何文章,其中一些是NewsPaper。

您可以添加新闻纸上这样的类别:

category = Categorie(title='Abc') 
category.save() 
news_paper = NewsPaper(slug='Something new', indexPosition=1, txt='...') 
news_paper.save() 
category.articles.add(news_paper) 

你可从特定类别的新闻报纸是这样的:

specific_category = Categorie.objects.get(title='Abc') 
NewsPaper.objects.filter(categorie_set=specific_category)