【问题标题】:Django: what is the best way to create models for students and tutors and both of them have different profiles?Django:为学生和导师创建模型的最佳方式是什么,并且他们都有不同的配置文件?
【发布时间】:2021-10-04 22:41:24
【问题描述】:

我为此做了很多搜索,甚至观看了一些教程,其中大多数都有不同的方法,这让我质疑自己现在所做的是否正确。但是我仍然尝试根据我在这些教程中学到的知识来实现​​我认为是最好的方法,但我需要对此进行一些验证。这就是我到目前为止所做的。

class User(AbstractUser):
    is_student = models.BooleanField(default=False)
    is_tutor = models.BooleanField(default=False)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    phone_number = models.CharField(max_length=11, blank=False, null=True)
    current_address = models.CharField(max_length=100, null=True)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')

class StudentProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    def __str__(self):
        return f'{self.user.username} Profile'

class TutorProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.CharField(max_length=255, blank=True)
    def __str__(self):
        return f'{self.user.username} Profile'

【问题讨论】:

  • 嘿,请解释一下different profiles?是什么意思。
  • 您的意思是您需要一种通过个人资料类型来区分用户的方法?
  • 看起来不错。它有什么问题?
  • @ShotikoAkhlouri 我的意思是,我希望拥有一些仅在导师的个人资料中才能找到的功能,例如收入、评级或简历。学生也有自己的个人资料,但与导师不同。对不起,如果我不清楚.. 对这些东西真的很陌生..
  • @Ersain 是的,我猜.. 我的意思是我只是想知道我是否采取了正确的方法来为多个用户创建个人资料。

标签: python django django-models orm abstractuser


【解决方案1】:

虽然您采用的方法效果不错,但仍有一些事项需要考虑。

  1. 虽然当前的方法似乎是通过将公共字段放在一个表中来适当使用,但从长远来看,数据库中将有多个条目供学生和导师使用。

例如,数据库有 1000 个学生条目和 20 个导师条目。 现在,会有

  • User 表中有 1020 个条目,
  • StudentProfile 表中的 1000 个条目仅包含用户 ID,并且
  • TutorProfile 表中有 20 个条目,包含用户 ID 和简历。
  1. User 模型中,当用户可以同时是学生和导师时,选择使用布尔字段is_studentis_tutor 区分学生和导师是有益的。如果他们不能这样,那么最好保留一个名为 user_type 的选择字段,并将学生和导师作为可能的选项。

  2. 通过学生和导师档案的单独模型,可以查询哪个用户是学生或导师。因此,在User 模型中使用布尔字段is_studentis_tutor 似乎是多余的。此外,如果您选择保留布尔字段进行检查,那么要获取查询集中的所有学生数据或所有导师数据,API 将不得不遍历所有 1020 条记录以检查布尔字段值并返回它。这将显着降低 API 速度并增加时间复杂度。

  3. 如果您要为用户显示User 表和TutorProfile 表中的所有信息。该查询需要获取与导师关联的特定用户的所有数据,然后将该数据与响应中 TutorProfile 中的数据一起发送。

对此的建议解决方法是,假设学生和导师被视为相互排斥的实体:

class User(AbstractUser):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    phone_number = models.CharField(max_length=11, blank=False, null=True)
    current_address = models.CharField(max_length=100, null=True)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')

    def __str__(self):
        return f'{self.username} Profile'

class StudentProfile(User):
    pass

class TutorProfile(User):
    bio = models.CharField(max_length=255, blank=True)

使用这种方法,将有两个模型StudentProfileTutorProfile 派生自User

要为学生创建实例,将在StudentProfile 中创建一个对象。同样,要为导师创建实例,将在TutorProfile 中创建一个对象。

因此,要查询所有学生或所有导师,需要向各自模型的 API 发出 GET 请求。

继续前面的例子,

在数据库中,对于 1000 个学生条目和 20 个导师条目,现在将有:

  • StudentProfile 表中有 1000 个条目,其中包含 User 模型的所有字段以及 StudentProfile 模型中的所有附加字段。
  • TutorProfile 表中的 20 个条目,包含User 模型的所有字段以及TutorProfile 模型中的所有附加字段,即bio

【讨论】:

    猜你喜欢
    • 2011-11-25
    • 2010-10-15
    • 1970-01-01
    • 2017-05-29
    • 2015-10-15
    • 1970-01-01
    • 2017-06-24
    • 2010-12-17
    • 1970-01-01
    相关资源
    最近更新 更多