0

ご覧ください:

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」と「NewsPaper」を「Article」として作成しました。(manyToMany)「Article」にリンクする「Category」クラスも作成します。

しかし、管理画面で「Category」から「CookRecette」や「​​NewsPaper」にリンクできません。コードから同じ。助けはありますか?

乾杯、
マーティン・マガキアン

PS: 申し訳ありませんが、実際にはこのコードは正しいです! 「Category」から「CookRecette」または「NewsPaper」を見ることができます。

4

2 に答える 2

1

「id」フィールドを定義する必要はありません。定義しない場合は、Django が自動的に追加します。

第 2 に、CookRecette および NewsPaper オブジェクトは、Category オブジェクトにリンクされていないため (ForeignKey、OneToOne、OneToMany、ManyToMany)、その方法でアクセスすることはできません。

任意の方法でモデルをリンクしたら、http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.InlineModelAdminを参照してください。 Djano 管理コンソールで関連オブジェクトをすばやく編集する方法を示します。

于 2011-03-03T22:12:44.920 に答える
0

NewsPaperその一部をArticleオブジェクトとして持っています。新しいオブジェクトを作成する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)
于 2011-03-03T22:29:32.043 に答える