Django开发者群  - 讨论区

标题:django admin中如何在一个模型中显示多对多外键模型属性

2010年05月28日 星期五 19:41

mysite.books.models

#coding=utf8
from django.db import models

# 所有字段都默认blank=False,

这使得它们不允许输入空值。
# 允许为空和默认为NULL值就加上如此属性:blank = True, null = True

class Publisher(models.Model):
    name = models.CharField('名称', max_length = 30)
    address = models.CharField('地址', max_length = 50)
    city = models.CharField('城市', max_length = 60)
    state_province = models.CharField('省份', max_length = 50)
    country = models.CharField('国家', max_length = 50)
    website = models.URLField('网站', blank = True) # 非必须项目
    class Meta:
        verbose_name = '出版商'
        verbose_name_plural = '出版商'
        ordering = ['name']
    def __unicode__(self):
        return self.name

class Author(models.Model):
    first_name = models.CharField('名', max_length = 30)
    last_name = models.CharField('姓', max_length = 40)
    email = models.EmailField('电子邮箱')
    class Meta:
        verbose_name = '作者'
        verbose_name_plural = '作者'
    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('出版日期')
    class Meta:
        verbose_name = '图书'
        verbose_name_plural = '图书'
        ordering = ['title', 'publisher']
    def __unicode__(self):
        return self.title

mysite.books.admin

#coding=utf8
from mysite.books.models import Publisher, Author, Book
from django.contrib import admin

class PublisherAdmin(admin.ModelAdmin):
    list_display = ('name', 'address', 'city', 'state_province', 'country', 'website')

class AuthorAdmin(admin.ModelAdmin):
    list_display = ('first_name','last_name', 'email')
    search_fields = ('first_name', 'last_name', 'email')
   
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'publisher', 'publication_date')
    list_filter = ('publication_date', 'publisher')
    # 搜索域
    search_fields = ('title', 'publisher__name') # 通过外键找到Publisher的name
    # 日期层次划分
    date_hierarchy = 'publication_date'
    # 必须是元组,记得加逗号
    ordering = ('-publication_date',)
    # 创建与修改图书信息需要填写的字段,按照以下的顺序显示
    fields = ('title', 'authors', 'publisher', 'publication_date')
   
admin.site.register(Publisher, PublisherAdmin)
admin.site.register(Author, AuthorAdmin)
admin.site.register(Book, BookAdmin)


Book与Author存在多对多关系,如何能够在django admin中直接显示一本书的所有作者啊?
谢谢各位!

2010年05月30日 星期日 22:24

def authors(obj):

    return str(obj.authors.all())

 

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'publisher', 'publication_date', authors) #authors没有引号是上面定义的函数

 

在Model的定义中加上

authors= models.ManyToManyField(Person, through='BookAuthor')

额外定义一个Model

class BookAuthor(models.Model):
book = models.ForeignKey(Book)
author = models.Foreignkey(Author)

具体参见一下下面的文档:
http://docs.djangoproject.com/en/dev/topics/db/models/

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号