显示模型及其关系

时间:2013-03-02 06:55:43

标签: python django django-models

我的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表中显示内容。我的要求是显示合并在一个页面中的两个内容。请帮我这样做。

2 个答案:

答案 0 :(得分: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 :(得分: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)

<强>模板

{% 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 %}