【问题标题】:How can i use 'set_password' in custom non-admin model?如何在自定义非管理员模型中使用“set_password”?
【发布时间】:2018-09-01 01:26:41
【问题描述】:

我想在django.contrib.auth.models 中使用来自用户模型的散列字段 set_password,我目前正在为此使用自定义用户模型。

我收到以下错误:Attribute error: 'User' object has no attribute 'set_password'

models.py

from django.db import models


class User(models.Model):
    first_name = models.CharField(max_length=50, blank=True)
    last_name = models.CharField(max_length=50, blank=True)
    profile_picture = 
     models.ImageField(upload_to="user_data/profile_picture", blank=True)
    username = models.CharField(max_length=100)
    birth_date = models.DateField(blank=True)
    gender = models.CharField(max_length=10, blank=True)
    password = models.CharField(max_length=1000)
    contact = models.CharField(max_length=10, blank=True)
    email = models.CharField(max_length=100)
    time_stamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.username

views.py

...
from .models import User

...
    def post(self, request):
        # Data is here
        form = self.form_class(request.POST)
        if form.is_valid():
            # create object of form
            user = form.save(commit=False)

            # cleaned/normalised data
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']

            # convert plain password into hashed
            user.set_password(user.password)
            user.save()

            return HttpResponse('Done here.')
            ...

forms.py(只是在 forms.py 中使用了一个小部件)

from .models import User
from django import forms


class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ['username', 'password']

【问题讨论】:

  • 您能补充一下您使用的表格吗?
  • 模型会更有用。它是自定义用户模型吗?它是否继承自抽象用户类之一?
  • 是的,它是一个自定义用户模型,我想在其中使用来自 contrib.auth.models 用户的散列字段
  • 请添加表格和型号
  • 行我说,你需要你的用户模型从 AbstractBaseUser 继承。

标签: python django django-models string-hashing


【解决方案1】:

这是一个非常容易解决的问题。只需像这样更改您的 models.py 文件:

from django.contrib.auth.models import AbstractBaseUser

class User(AbstractBaseUser):
    first_name = models.CharField(max_length=50, blank=True)
    last_name = models.CharField(max_length=50, blank=True)
    profile_picture = models.ImageField(upload_to="user_data/profile_picture", blank=True)
    username = models.CharField(max_length=100)
    birth_date = models.DateField(blank=True)
    gender = models.CharField(max_length=10, blank=True)
    password = models.CharField(max_length=1000)
    contact = models.CharField(max_length=10, blank=True)
    email = models.CharField(max_length=100)
    time_stamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.username

这样,您的用户模型将继承所有 AbstractBaseUser 方法,包括 set_password

查看文档中的full example 以获取更多信息。

【讨论】:

  • 我覆盖了 AbstractBaseUser 和 BaseUserManager。这是我自己的喜好。但上述将运作良好。谢谢 Gal Silberman
猜你喜欢
  • 2011-09-14
  • 1970-01-01
  • 2017-02-08
  • 2014-07-11
  • 2013-06-26
  • 2013-06-01
  • 2019-10-05
  • 2011-09-28
  • 2014-10-21
相关资源
最近更新 更多