Django:失去理智,用 Django auth 拔掉我的头发
我正在创建一个 SAAS 作为一个项目,但似乎无法将我的恐龙大脑包裹在这个身份验证系统中。在我意识到你可以添加到正常的身份验证系统之前,我开始推出自己的系统。这是完全错误的吗?我是否应该以某种方式扩展用户模型,但仍然包含我自己的所有属性(用户名、密码等)?
from django.db import models
from django.contrib.auth.models import User
from annoying.fields import AutoOneToOneField
from myproject.core.modelfields import LowerAlphanumericField
from myproject.apps.account.models import Account
class Administrator(models.Model):
"""
Administrator for an Account (application holds multiple accounts as it is a SAAS).
"""
account = models.ForeignKey(Account)
user = AutoOneToOneField(User, primary_key=True)
name = models.CharField(max_length=255)
email = models.EmailField()
username = LowerAlphanumericField(max_length=30)
password = models.CharField(max_length=255)
如果我访问 http://127.0.0.1:8080/admin/auth/user/ 3/ 我收到错误,但我创建的第三个管理员对象的主键是 3(假设它是相关 User 对象的主键。我是否遗漏了某些内容。另外,我是否需要创建一个密码字段,以及这里的所有垃圾,或者更确切地说,我应该吗?
I'm creating a SAAS as a project, and can't seem to wrap my dinosaur brain around this auth system. I started to roll my own system before I realized you could add on to the normal auth system. Is this completely wrong? Should I somehow extend the User model but still include all of my own attributes (username, password, etc)?
from django.db import models
from django.contrib.auth.models import User
from annoying.fields import AutoOneToOneField
from myproject.core.modelfields import LowerAlphanumericField
from myproject.apps.account.models import Account
class Administrator(models.Model):
"""
Administrator for an Account (application holds multiple accounts as it is a SAAS).
"""
account = models.ForeignKey(Account)
user = AutoOneToOneField(User, primary_key=True)
name = models.CharField(max_length=255)
email = models.EmailField()
username = LowerAlphanumericField(max_length=30)
password = models.CharField(max_length=255)
If I visit http://127.0.0.1:8080/admin/auth/user/3/ I get an error, but the primary key for the third administrator object I created is 3 (which one would assume is the primary key for the related User object. Am I missing something. Also, do I need to create a password field, and all that junk here, or rather should I?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,你真的不需要添加那么多额外的信息。 Django auth 涵盖了您正在寻找的所有方面:
并且 Django auth 还有一个相当有用的权限系统:
每当我希望向 User 对象添加附加信息时,我通常会创建一个新模型并存储对用户的引用。
然后按照 @bste 的建议,将
AUTH_PROFILE_MODULE = 'accounts.UserProfile'
添加到您的设置文件中。这允许您使用任何User
对象上的get_profile()
方法访问用户配置文件(您的额外信息)。It seams to me that you don't really need to add that much extra information. Django auth covers all the aspects you are looking for:
and Django auth also has a fairly useful permissions system:
Whenever I wish to add additional information to a User object, I generally create a new model and store a reference to the User.
And then follow what @bste suggests, add
AUTH_PROFILE_MODULE = 'accounts.UserProfile'
to your settings file. This allows you to access a user profile (your extra information) with theget_profile()
method on anyUser
object.您是否已经在使用内置 django 身份验证?如果是,那么您可以指定一个与用户模型相关的模型,您可以在其中存储有关用户的其他信息。
此处解释: http://docs .djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
添加以下行很简单:
无需自己存储密码,我认为 django 会这样做
are you already using built-in django authentication? if yes, then you can specify a model that's related to the User model in which you can store additional information about users.
it's explained here: http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
it's simple adding the following line:
there's no need to store the passwrd yourself, i think django does this