0

我的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 显示了Book 类中的内容,但是我无法显示author 表中的内容。我的要求是在一个页面中显示合并的两个内容。请帮我做这件事。

4

2 回答 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 %}
于 2013-03-02T07:02:35.723 回答
1

模型.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)

视图.py

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

模板

{% 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 %}
于 2013-03-02T07:21:27.850 回答