0

我有两个型号

class Reporter(models.Model):
    full_name = models.CharField(max_length=70)
    # On Python 3: def __str__(self):
    def __unicode__(self):
        return self.full_name
class Article(models.Model):
    pub_date = models.DateField()
    headline = models.CharField(max_length=200)
    content = models.TextField()
    reporter = models.ForeignKey(Reporter)

现在在管理站点中,我想注册记者的模型,然后单击它时,可以从该记者的模型中添加文章模型对象.. 有可能吗?如果是的话如何做到这一点?

4

1 回答 1

0

您需要内联模型管理员。在您的admin.py中,添加以下内容:

from django.contrib import admin
from .models import Article, Reporter

class ArticleInline(admin.TabularInline):
    model = Article

class ReporterAdmin(admin.ModelAdmin):
    inlines = [
        ArticleInline,
    ]

admin.site.register(Reporter, ReporterAdmin)
于 2013-09-24T05:24:11.040 回答