2011-03-03 73 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)