简体   繁体   English

如何创建自己的模型,可以像 django 中的管理员用户一样进行身份验证

[英]How to create own model that can authenticate like admin user in django

I create own model user but can't authenticate it like in admin user我创建了自己的模型用户,但无法像管理员用户一样对其进行身份验证

# myapss/models.py
from django.db import models
from django.contrib.auth.hashers import make_password, check_password
from django.db.models.manager import Manager

class MyOwnManager(Manager):
    ...
class MyOwnUser(models.Model):
    username = models.CharField(max_length=50, unique=True)
    password = models.CharField(max_length=255)

    ...

    objects = MyOwnManager()

I want to authenticate this model without using AbstractUser or AbstractBaseUser我想在不使用 AbstractUser 或 AbstractBaseUser 的情况下验证此模型

#myapps/views.py
from .models import MyOwnUser

def login(request):
    # Authentication
  1. There are certain steps that you need to follow in order to create a custom user model in Django.为了在 Django 中创建自定义用户模型,您需要遵循某些步骤。 Please have a look at the following document and find the detailed instruction on what you have to do in order to develop a custom user model:请查看以下文档并找到有关开发自定义用户模型必须执行的操作的详细说明:

https://docs.djangoproject.com/en/3.2/topics/auth/customizing/#substituting-a-custom-user-model https://docs.djangoproject.com/en/3.2/topics/auth/customizing/#substituting-a-custom-user-model

  1. I would also recommend to have a look at this tutorial:我还建议您查看本教程:

https://testdriven.io/blog/django-custom-user-model/ https://testdriven.io/blog/django-custom-user-model/

  1. You are trying to build a User mode from scratch, it is quite complicated and redundant, because, effectively you will end up writing your own code reproducing AbstractBaseUser class functionality.您正在尝试从头开始构建用户模式,它非常复杂和冗余,因为实际上您最终将编写自己的代码来重现 AbstractBaseUser 类功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Django 中为管理员用户和普通用户创建单独的模型(表) - How to create separate model(table) for Admin User and Normal User in Django 如何在Django中创建管理员用户并将其链接到模型? - how do i create an admin user in django and link it to a model? 如何在 Django 中使用自定义 model 对用户进行身份验证? - How to authenticate user using custom model in Django? Django admin-在用户模型上创建复杂的过滤器 - Django admin - Create a complex filter on User model 如何在Django管理员页面的单元测试中对用户进行身份验证? - How to authenticate a user in a unit test for the Django admin page? 使用Django User-Model还是创建自己的Model? - Use Django User-Model or create a own Model? django authenticate()用于定制用户模型 - django authenticate() for custom user model 如何以编程方式对 Django 中的用户进行身份验证? - How can I programmatically authenticate a user in Django? 如何在 django 用户 model 中生成随机唯一用户 ID 值并将其显示在 django 管理员上? - How can I generate a random unique User id value in django user model and show it on django admin? 可以在Django中使用“ authenticate”吗? - Can `authenticate` use like this in django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM