如何从另一个模型引用 Django 模型

发布于 2024-09-02 12:38:53 字数 1278 浏览 3 评论 0原文

我希望在管理面板中为一个测试程序创建一个视图,该程序记录书籍、出版商和作者(如在 djangoproject.com 上)

我定义了以下两个模型。

class Author(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField()

    def __unicode__(self):
        return u'%s %s' % (self.first_name, self.last_name)

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

    def __unicode__(self):
        return self.title

我想要做的是更改 Book 模型以引用任何作者的名字,并使用 admin.AdminModels 来显示它。

#Here is the admin model I've created.

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'publisher', 'publication_date') # Author would in here
    list_filter = ('publication_date',)
    date_hierarchy = 'publication_date'
    ordering = ('-publication_date',)
    fields = ('title', 'authors', 'publisher', 'publication_date')
    filter_horizontal = ('authors',)
    raw_id_fields = ('publisher',)

据我了解,同一模型中不能有两个外键。谁能给我举个例子来说明如何做到这一点?

我尝试过很多不同的事情,这让我一整天都发疯。我对 Python/Django 还很陌生。

需要明确的是 - 我只是希望作者的名字/姓氏出现在书名和出版商名称旁边。

谢谢

Im looking to create a view in the admin panel for a test program which logs Books, publishers and authors (as on djangoproject.com)

I have the following two models defined.

class Author(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField()

    def __unicode__(self):
        return u'%s %s' % (self.first_name, self.last_name)

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

    def __unicode__(self):
        return self.title

What I want to do, is change the Book model to reference the first_name of any authors and show this using admin.AdminModels.

#Here is the admin model I've created.

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'publisher', 'publication_date') # Author would in here
    list_filter = ('publication_date',)
    date_hierarchy = 'publication_date'
    ordering = ('-publication_date',)
    fields = ('title', 'authors', 'publisher', 'publication_date')
    filter_horizontal = ('authors',)
    raw_id_fields = ('publisher',)

As I understand it, you cannot have two ForeignKeys in the same model. Can anyone give me an example of how to do this?

I've tried loads of different things and its been driving me mad all day. Im pretty new to Python/Django.

Just to be clear - I'd simply like the Author(s) First/Last name to appear alongside the book title and publisher name.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独守阴晴ぅ圆缺 2024-09-09 12:38:53

一个模型上可以有多个外键。
如果您将外键字段的名称放入 list_display 中,您将始终只看到关联模型的 __unicode__ 表示形式。
但您可以向 BookAdmin 添加这样的函数:

def first_names(self, obj):
    return ','.join(a.first_name for a in obj.authors.all())
get_sites.short_description = 'First Names'

然后将 'first_names' 添加到 list_display

You can have more than one Foreign Key on a model.
If you would put a Foreign-Key field's name in list_display you will always just see the __unicode__ representation of the associated model.
But you can add a function like this to your BookAdmin:

def first_names(self, obj):
    return ','.join(a.first_name for a in obj.authors.all())
get_sites.short_description = 'First Names'

Then add 'first_names' to list_display.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文