Django:失去理智,用 Django auth 拔掉我的头发

发布于 2024-08-22 06:59:46 字数 1036 浏览 7 评论 0原文

我正在创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰認得朕 2024-08-29 06:59:46

在我看来,你真的不需要添加那么多额外的信息。 Django auth 涵盖了您正在寻找的所有方面:

  • 用户名
  • 密码(sha1 哈希)
  • 名字
  • 姓氏
  • 电子邮件

并且 Django auth 还有一个相当有用的权限系统:

  • 超级用户
  • 工作人员
  • 活跃

每当我希望向 User 对象添加附加信息时,我通常会创建一个新模型并存储对用户的引用。

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    # Extra functionality

然后按照 @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:

  • username
  • password (sha1 hash)
  • firstname
  • lastname
  • email

and Django auth also has a fairly useful permissions system:

  • superuser
  • staff
  • active

Whenever I wish to add additional information to a User object, I generally create a new model and store a reference to the User.

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    # Extra functionality

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 the get_profile() method on any User object.

笛声青案梦长安 2024-08-29 06:59:46

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:

AUTH_PROFILE_MODULE = 'accounts.Adminstrator'

there's no need to store the passwrd yourself, i think django does this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文