显示模型及其关系

0 投票
2 回答
524 浏览
提问于 2025-04-17 17:40

我的models.py文件内容如下:

from django.db import models

class Book(models.Model):
    book_id=models.AutoField(primary_key=True,unique=True)
    book_name=models.CharField(max_length=30)
    author_name=models.CharField(max_length=30)
    publisher_name=models.CharField(max_length=40)
    def __unicode__(self):
        return "%d %s %s %s" % (self.book_id,self.book_name, self.author_name,self.publisher_name)


class Author(models.Model):
    author_id=models.AutoField(primary_key=True)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()
    age=models.IntegerField()
    book=models.ForeignKey(Book)

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

而我的views.py文件内容是这样的:

def index(request):

    books = Book.objects.all()
    context={'books':books}
    return render(request,'index.html', context)

上面的views.py可以显示书籍类的内容,但我无法显示作者表中的内容。我想要的效果是把这两部分内容合并在同一个页面上显示。请帮我实现这个功能。

2 个回答

2

首先,看起来你没有正确设置你的模型。Django会自动处理ID,如果你不提供的话,所以你不需要写 author_id=models.AutoField(primary_key=True)。另外,书籍应该有一个作者,而不是作者有一本书,对吧?如果你设置得正确,你也不需要 author_name=models.CharField(max_length=30),因为你会有一个指向作者模型的外键,那个模型里已经有作者的名字了。

一旦你正确设置了这些,访问模板中的数据就非常简单了。例如,在你的模板中,你可以用 book.author.first_name 来获取这本书作者的名字。


from django.db import models

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

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

class Book(models.Model):
    name = models.CharField(max_length=30)
    author = models.ForeignKey(Author)
    publisher_name = models.CharField(max_length=40)

    def __unicode__(self):
        return u"%d %s %s %s" % (self.pk, self.name, self.author.name, self.publisher_name)

模板

{% for book in books %}
    ...
    {% for author in book.author_set %}
        ...
    {% endfor %}
{% endfor %}
1

models.py

class Book(models.Model):
    book_name=models.CharField(max_length=30)
    author_name=models.CharField(max_length=30)
    publisher_name=models.CharField(max_length=40)
    author=models.ForeignKey(Author)

    def __unicode__(self):
        ..........

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

    def __unicode__(self):
        ........

    def books(self):
        return Book.objects.filter(author=self)

这个文件通常用来定义你的数据模型,也就是你要存储的数据结构。在这里,你可以告诉程序你需要哪些数据,比如用户的信息、产品的详情等等。简单来说,就是告诉程序“我想要什么样的数据”。

views.py

def index(request):
    authors = Author.objects.all()
    context={'authors':authors}
    return render(request,'index.html', context)

这个文件负责处理用户的请求和返回的响应。可以把它想象成一个中介,用户通过网页发出请求,views.py会处理这些请求,然后决定要显示什么内容给用户。它就像是一个服务员,接收点单并把食物送到桌子上。

template

{% for author in authors %}
    Author: {{author.first_name}} {{author.last_name}}<br/>
    Email: {{author.email}}
    Age: {{author.age}}
    Books:
        {% for book in author.books %}
            .......
        {% endfor %}
{% endfor %}

模板文件用来定义网页的外观和布局。这里面包含了HTML代码,决定了用户在浏览器中看到的内容和样式。可以把它想象成网页的“衣服”,让网页看起来更好看、更有吸引力。

撰写回答