Python Django 夹层模型导入

Python Django Mezzanine Models Import

from django.db import models
from mezzanine.pages.models import Page

# The members of Page will be inherited by the Author model, such
# as title, slug, etc. For authors we can use the title field to
# store the author's name. For our model definition, we just add
# any extra fields that aren't part of the Page model, in this
# case, date of birth.

class Author(Page):
    dob = models.DateField("Date of birth")

class Book(models.Model):
    author = models.ForeignKey("Author")
    cover = models.ImageField(upload_to="authors")

那么Book是不是也继承了Page的属性呢?所以这意味着任何 属性 或页面的方法都可以从上面的代码访问?

如果要扩展模型,最好使用外键关系。您可以使用与包含字段的模型的一对一关系以获取更多信息。例如:

class Author(models.Model):
    page = models.OneToOneField(Page)
    dob = models.DateField("Date of birth")

您可以使用 Django 的标准相关模型约定访问相关信息:

a = Author.objects.get(...) 
name = a.page.title # author's name is stored in Page.title field