简体   繁体   中英

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

#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. 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

  1. I would also recommend to have a look at this tutorial:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM