Django用户模型(Django user model)

编程入门 行业动态 更新时间:2024-10-27 01:29:21
Django用户模型(Django user model)

我有一个Django应用程序,我扩展了用户模型。 (见下文)。 我有一种类型的用户有3个属性 - 电子邮件,全名和头像。 我将有多种类型的用户。 也许老师,学生,家长,校友等(不是我的领域,而是一个例子)。 我是否应该在下面的用户模型中放置它们都具有的领域,然后创建一个具有4个不共享的属性的模型(例如,您所教的年级,或者学校的年级,毕业的年份?在飞行中,但你明白我的意思)与外键加入他们。 我正在尝试扩展此页面 。 我不确定related_name arg的含义。

class Teach(AbstractBaseUser): user = models.ForeignKey(User, related_name='teacher') classes = models.EmailField(...) year_started_teaching = models.. class Student(AbstractBaseUser): user = models.ForeignKey(User, related_name='teacher') year_graduating = models.Integer() class User(AbstractBaseUser): email = models.EmailField(max_length=255, unique=True) full_name = models.CharField(max_length=255, blank=True, null=True) avatar = models.ImageField(upload_to='images/', blank=True) is_active = models.BooleanField(default=True) # can login staff = models.BooleanField(default=False) # staff user non superuser admin = models.BooleanField(default=False) # superuser timestamp = models.DateTimeField(auto_now_add=True)

或者我应该有4个独立的模型,每个模型都有用户。 在某些时候,我会列出所有想要过滤的用户,因此我认为有主要模型和模型。

class Teach(AbstractBaseUser): email = models.EmailField(max_length=255, unique=True) full_name = models.CharField(max_length=255, blank=True, classes = models.EmailField(...) year_started_teaching = models.. class Student(AbstractBaseUser): email = models.EmailField(max_length=255, unique=True) full_name = models.CharField(max_length=255, blank=True) year_graduating = models.Integer(...)

我很确定我不应该只有一个带有标志的模型来表示用户类型。

I have a Django app where I have extended the user model. (see below). I have one type of user that has 3 properties - email, full_name and an avatar. I am going to be having multiple types of users. Maybe Teachers, Students, Parents, Alumni, etc (not my field, but an example). Should I put fields that they all have in common in this below User model and then create a model that has properties that the 4 don't share (e.g. grade you teach, or year in school, year graduated? I'm making this up on the fly, but you see what I mean) with a foreign key to join them. I'm trying to extend this page . I'm not sure what the related_name arg means.

class Teach(AbstractBaseUser): user = models.ForeignKey(User, related_name='teacher') classes = models.EmailField(...) year_started_teaching = models.. class Student(AbstractBaseUser): user = models.ForeignKey(User, related_name='teacher') year_graduating = models.Integer() class User(AbstractBaseUser): email = models.EmailField(max_length=255, unique=True) full_name = models.CharField(max_length=255, blank=True, null=True) avatar = models.ImageField(upload_to='images/', blank=True) is_active = models.BooleanField(default=True) # can login staff = models.BooleanField(default=False) # staff user non superuser admin = models.BooleanField(default=False) # superuser timestamp = models.DateTimeField(auto_now_add=True)

Or should I have 4 separate models that each have the users in it. At some point, I'll have a list of all users that I want to filter by and so I think having the main model and the model.

class Teach(AbstractBaseUser): email = models.EmailField(max_length=255, unique=True) full_name = models.CharField(max_length=255, blank=True, classes = models.EmailField(...) year_started_teaching = models.. class Student(AbstractBaseUser): email = models.EmailField(max_length=255, unique=True) full_name = models.CharField(max_length=255, blank=True) year_graduating = models.Integer(...)

I'm pretty sure I shouldn't have just one model with flags to denote user types.

最满意答案

您应该只有一个从AbstractBaseUser或AbstractUser继承的用户模型。 我相信你的情况是使用OneToOne关系的更好选择。 例如:

class Profile(AbstractBaseUser): """ shared extra fields for all users """ email = models.EmailField(max_length=255, unique=True) full_name = models.CharField(max_length=255, blank=True) class Student(models.Model): profile = models.OneToOneField(Profile, on_delete=models.CASCADE, ) year_graduating = models.Integer(...) class Teacher(models.Model): profile = models.OneToOneField(Profile, on_delete=models.CASCADE, ) year_started_teaching = models.Integer(...)

在你的settigs.py中:

AUTH_USER_MODEL = 'myapp.Profile'

正如你所看到的AUTH_USER_MODEL--应该是一个单一的模型,这就是为什么从AbstractBaseUser继承几个模型是一个坏主意。 否则创建用户认证系统将成为一个非常头疼的问题。 唯一的缺点是 - 有可能拥有两种类型的用户 - 教师和学生,但从我的观点来看,这不是什么大问题,也很容易检查。

另一种选择是使用通用关系:

https://docs.djangoproject.com/en/2.0/ref/contrib/contenttypes/#generic-relations

但是在代码库中管理起来会更困难。

You should have only a single user model inherited from AbstractBaseUser or AbstractUser. I believe in your case it`s a better option to use OneToOne relation. For example:

class Profile(AbstractBaseUser): """ shared extra fields for all users """ email = models.EmailField(max_length=255, unique=True) full_name = models.CharField(max_length=255, blank=True) class Student(models.Model): profile = models.OneToOneField(Profile, on_delete=models.CASCADE, ) year_graduating = models.Integer(...) class Teacher(models.Model): profile = models.OneToOneField(Profile, on_delete=models.CASCADE, ) year_started_teaching = models.Integer(...)

And in your settigs.py:

AUTH_USER_MODEL = 'myapp.Profile'

As you can see AUTH_USER_MODEL - supposed to be a single model, thats why it's a bad idea to have several models inherited from AbstractBaseUser. Otherwise creating user authentication system will become a huge headache. The only drawback - is it would be possible to have user of both types - Teacher and Student, but for my point of view it`s not a big deal and very easy to check.

Another option is to use Generic relations:

https://docs.djangoproject.com/en/2.0/ref/contrib/contenttypes/#generic-relations

But it`ll make more difficult to manage in your code base.

更多推荐

本文发布于:2023-07-22 19:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222785.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   用户   Django   user   model

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!