Python Django遗传

Python Django遗传,python,django,model-inheritance,Python,Django,Model Inheritance,请看一看: 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',

请看一看:

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”和“报纸”作为“文章”。 我还创建了一个“Categorie”类,该类链接到(许多)“Article”

但在管理界面中,我无法将“分类”链接到“CookRecette”或“报纸”。 代码中也一样。 有什么帮助吗

干杯,
马丁马加基安


我很抱歉,但实际上这个代码是正确的!所以一切正常,我可以从“分类”中看到我的“CookRecette”或“Paper”

我首先要说的是,你不需要定义“id”字段,如果你不定义它,Django会自动添加它

其次,CookRecette和Paper对象没有通过任何方式(ForeignKey、OneToOne、OneToMany、ManyToMany)链接到Category对象,因此无论如何都无法通过这种方式访问它们


在以任何方式将模型链接在一起后,您可能希望查看一下将向您展示如何在Djano管理控制台中快速编辑相关对象的方法。

报纸
的一部分作为
文章
对象。如果要创建新的
报纸
对象,您将在文章中看到一个新对象。所以在管理界面中,当管理类别时,您可以选择任何文章,其中一些是报纸

您可以将报纸添加到如下类别:

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)
您可以从特定类别检索新闻稿,如下所示:

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)