扩展 Django 1.11 用户模型

Posted

技术标签:

【中文标题】扩展 Django 1.11 用户模型【英文标题】:Extending the Django 1.11 User Model 【发布时间】:2018-04-24 17:27:31 【问题描述】:

我正在尝试研究如何扩展 Django 用户模型以向用户添加信息。我似乎无法让它工作。我究竟做错了什么? 在我扩展到的同一模型中可以有外键吗?如何创建超级用户,还是必须通过python manage.py shell 手动创建?

到目前为止,这是我的代码:

class PersonModel(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    firstName = models.CharField(max_length=50)
    lastName = models.CharField(max_length=50)
    company = models.ForeignKey(CompanyModel, on_delete=models.CASCADE, null=True)
    phone = models.ForeignKey(PhoneModel, on_delete=models.CASCADE, null=True)
    email = models.EmailField(blank=True)

    def __str__(self):
        return '%s %s - %s - %s, %s' % (self.firstName, self.lastName,
                                        self.company, self.phone, self.email
                                       )

    class Meta:
        ordering = ['firstName']
        verbose_name = "Customer Contact Information"
        #verbose_name_plural = "Contacts"

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        PersonModel.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

更新(最终): 在 raratiru 的帮助下,我基本上能够让他分享的脚本继续运行。由于我的外键要求,我仍然难以创建超级用户。

from django.contrib.auth.models import (
    AbstractBaseUser,
    PermissionsMixin,
    BaseUserManager,
)
from django.core.mail import send_mail
from django.db import models
from django.utils.translation import ugettext_lazy as _
from customers import models as customers_models

class TravelModel(models.Model):
   mileageRate = models.DecimalField(max_digits=4, decimal_places=3)

   def __str__(self):
      return '%s' % (self.mileageRate)

   class Meta:
      verbose_name = "Current Federal Milage Rate"
      #verbose_name_plural = "Milage"


class UserManager(BaseUserManager):
    def create_user(self, email, firstName, lastName, company, phone, password=None, **kwargs):
        email = self.normalize_email(email)
        user = self.model(email=email, **kwargs)
        user.firstName = firstName
        user.lastName = lastName
        user.company = company
        user.phone = phone
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, firstName, lastName, password=None, **kwargs):
        #user = self.create_user(**kwargs)
        email = self.normalize_email(email)
        user = self.model(email=email, **kwargs)
        user.firstName = firstName
        user.lastName = lastName
        user.set_password(password)
        user.is_superuser = True
        user.is_staff = True
        user.save(using=self._db)
        return user


class AliasField(models.Field):
    def contribute_to_class(self, cls, name, virtual_only=False):
        super().contribute_to_class(cls, name, virtual_only=True)
        setattr(cls, name, self)

    def __get__(self, instance, instance_type=None):
        return getattr(instance, self.db_column)


class MyUser(AbstractBaseUser, PermissionsMixin):
      firstName = models.CharField(max_length=50, blank=False, null=False)
      lastName = models.CharField(max_length=50, blank=False, null=False)
      company = models.ForeignKey(customers_models.CompanyModel, on_delete=models.PROTECT, null=False)
      phone = models.ForeignKey(customers_models.PhoneModel, on_delete=models.PROTECT, null=False)

      email = models.EmailField(_('email address'), max_length=255, unique=True)

      is_staff = models.BooleanField(
            _('staff status'),
            default=False,
            help_text=_(
                  'Designates whether the user can log into this admin '
                  'site.'
            )
      )
      is_active = models.BooleanField(
            _('active'),
            default=True,
            help_text=_(
                  'Designates whether this user should be treated as '
                  'active. Unselect this instead of deleting accounts.'
            )
      )
      username = AliasField(db_column='email')

      objects = UserManager()

      USERNAME_FIELD = 'email'
      REQUIRED_FIELDS = ['firstName','lastName',]

      class Meta(object):
            ordering = ['firstName']
            verbose_name = _('Contact')
            verbose_name_plural = _('Contacts')

      def __str__(self):
            return '%s - %s %s - %s - %s' % (self.company, self.firstName, self.lastName, self.email, self.phone)

      def get_full_name(self):
            return self.email

      def get_short_name(self):
            return self.email

      def email_user(self, subject, message, from_email=None, **kwargs):
            """
            Sends an email to this User.
            """
            send_mail(subject, message, from_email, [self.email], **kwargs)

为了避免外键问题,最简单的解决方案是在创建超级用户之前删除 null=False 要求 - 之后分配公司和电话 - 然后将 null 设置回 false。

【问题讨论】:

【参考方案1】:

这就是我扩展用户模型的方式。以下代码还将用户名替换为电子邮件字段。我发布它是因为它会导致核心更改,因此它可以明确背后的逻辑。

基本思想可以在this post 中找到和解释。一个很不错的帖子也可以找到here。

在这种情况下,AliasField 创建字段username 作为email 的别名。虽然这不是必需的,因为 django 已经记录了查找用户模型及其相关字段的proper way。

from django.contrib.auth.models import (
    AbstractBaseUser,
    PermissionsMixin,
    BaseUserManager,
)
from django.core.mail import send_mail
from django.db import models
from django.utils.translation import ugettext_lazy as _


class UserManager(BaseUserManager):
    def create_user(self, email, password=None, **kwargs):
        email = self.normalize_email(email)
        user = self.model(email=email, **kwargs)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, **kwargs):
        user = self.create_user(**kwargs)
        user.is_superuser = True
        user.is_staff = True
        user.save(using=self._db)
        return user


class AliasField(models.Field):
    def contribute_to_class(self, cls, name, private_only=False):
        super().contribute_to_class(cls, name, private_only=True)
        setattr(cls, name, self)

    def __get__(self, instance, instance_type=None):
        return getattr(instance, self.db_column)


class MyUser(AbstractBaseUser, PermissionsMixin):
    custom_field = models.ForeignKey(
        'app.Model',
        on_delete=models.PROTECT,
    )

    email = models.EmailField(_('email address'), max_length=255, unique=True)

    is_staff = models.BooleanField(
        _('staff status'),
        default=False,
        help_text=_(
            'Designates whether the user can log into this admin '
            'site.'
        )
    )
    is_active = models.BooleanField(
        _('active'),
        default=True,
        help_text=_(
            'Designates whether this user should be treated as '
            'active. Unselect this instead of deleting accounts.'
        )
    )
    username = AliasField(db_column='email')

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['user_branch_key', ]

    class Meta(object):
        ordering = ['email']
        verbose_name = _('My User')
        verbose_name_plural = _('My User')

    def __str__(self):
        return 'id: 0 - 1, 2'.format(self.id, self.email, self.user_branch_key)

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.email

    def email_user(self, subject, message, from_email=None, **kwargs):
        """
        Sends an email to this User.
        """
        send_mail(subject, message, from_email, [self.email], **kwargs)

一旦您扩展了您的用户模型,例如在文件./the_user/models.py 中名称为the_user 的应用程序中,您必须在settings.py 文件中进行一些更改:

INSTALLED_APPS注册应用程序 ./manage.py makemigrations && ./manage.py migrate 按照the docs 中的说明设置AUTH_USER_MODEL = 'the_user.MyUser

这样,在另一个模型中,您可以按如下方式添加外键:

from the_user.models import MyUser
from django.conf import settings
from django.db import models

class AModel(models.Model)
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.PROTECT,
        related_name='%(app_label)s_%(class)s_user'
    )

【讨论】:

似乎不是在扩展,而是在重写 django 用户模型。这比关于这个主题的 Django 文档要详细得多:( @Jaberwocky 确实如此。我正在搜索代码以进行核心更改(使用电子邮件而不是用户名),上面说明了用户模型背后的逻辑。 感谢您的贡献。我觉得这并没有完全回答我的问题。你能吗,如果是的话,你如何将外键关系与用户联系起来?更详细地说,如果我想将我拥有的内容存储在PersonModel 中,如何使用您的方法来完成? 我相信我在docs.djangoproject.com/en/1.11/topics/auth/customizing/… 中找到了我的其余答案。谢谢! @Jaberwocky 确实如此。您还可以将模型作为内联添加到现有用户管理员中,而不是像我一样重写它。我更新了我的答案以说明如何添加外键。

以上是关于扩展 Django 1.11 用户模型的主要内容,如果未能解决你的问题,请参考以下文章

Django 1.11 模型迁移操作不适用

Django-1.11中文文档-模型Models

通过 ForeignKey 模型计算项目,Django 1.11

Django开发bug及问题记录

Django 1.11 - 无法在管理面板中编辑我的模型

Django 1.11 如何从保存在 CMS 站点模型中的 HTML 中显示模板变量