首页 > 解决方案 > 书籍、个人资料和作者之间的关系在 Django 中是否正确

问题描述

我创建了 3 个表配置文件、作者和书籍。这三个表之间的关系在Django中是否正确。

从 django.db 导入模型

# It is a user profile 
class Profile(models.Model):
    username = models.CharField(max_length=250)
    firstname = models.CharField(max_lenght=250)
    lastname = models.CharField(max_length=250)
    useremail = models.EmailField()

    def __str__(self):
        return self.username
# It is a author table
class Author(models.Model):
    authorname = models.CharField(max_length=250)
    authoremail = models.EmailField()
    authormobile = models.IntegerField()
    authoraffliation = models.CharField(max_length=250)

    def __str__(self):
        return self.authorname
# It is a book database 
class Book(models.Model):
    bookname = models.CharField(max_length=250)
    bookuser = models.ForeignKey(Profile)
    bookauthor = models.ManyToManyField(Author)
    publisher = models.CharField(max_length=250)
    published_year = models.DateField()
    isbn_number = models.CharField(max_length=250)

    # The __str__ method just tells django what to print when it need to print out an instance of Book model  
    def __str__(self):
        return self.bookname

标签: django-modelsmodelschema

解决方案


推荐阅读