简体   繁体   English

如何在 django 用户 model 中生成随机唯一用户 ID 值并将其显示在 django 管理员上?

[英]How can I generate a random unique User id value in django user model and show it on django admin?

I have a Model in Django.我在 Django 中有一个 Model。 I just extended the AbstractBaseUser and added some custom fields in the with BaseUserManager.我刚刚扩展了 AbstractBaseUser 并在 BaseUserManager 中添加了一些自定义字段。

Now I need to generate a unique Id as primary key and need to show it on Django admin.现在我需要生成一个唯一的 Id 作为主键,并需要在 Django 管理员上显示它。 How can I do that?我怎样才能做到这一点? My model is given below.我的 model 如下所示。

from django.db import models
from django.contrib.auth.models import AbstractBaseUser,PermissionsMixin,BaseUserManager



class CustomUserManager(BaseUserManager):
    def _create_user(self, email, password, first_name, last_name, mobile, **extra_fields):
        if not email:
            raise ValueError("Email must be provided")
        if not password:
            raise ValueError('Password is not provided')

        user = self.model(
            email = self.normalize_email(email),
            first_name = first_name,
            last_name = last_name,
            mobile = mobile,
            **extra_fields
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password, first_name, last_name, mobile, **extra_fields):
        extra_fields.setdefault('is_staff',True)
        extra_fields.setdefault('is_active',True)
        extra_fields.setdefault('is_superuser',False)
        return self._create_user(email, password, first_name, last_name, mobile, password, 
**extra_fields)

    def create_superuser(self, email, password, first_name, last_name, mobile, 
**extra_fields):
        extra_fields.setdefault('is_staff',True)
        extra_fields.setdefault('is_active',True)
        extra_fields.setdefault('is_superuser',True)
        return self._create_user(email, password, first_name, last_name, mobile, 
 **extra_fields)


class User(AbstractBaseUser,PermissionsMixin):
    
    email = models.EmailField(db_index=True, unique=True, max_length=254)
    first_name = models.CharField(max_length=240)
    last_name = models.CharField(max_length=255)
    mobile = models.CharField(max_length=50)
    address = models.CharField( max_length=250)

    is_staff = models.BooleanField(default=True) 
    is_active = models.BooleanField(default=True) 
    is_superuser = models.BooleanField(default=False) 

    objects = CustomUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name','last_name','mobile']

    class Meta:
        verbose_name = 'User'
        verbose_name_plural = 'Users'

If you've set DEFAULT_AUTO_FIELD inside your settings.py then you don't have to set primary key for your User model django will add it automaticaly you can read about it primary_key but if you're willing to add custom id's to your User model then you can use UUIDField like this如果您已经primary_key设置DEFAULT_AUTO_FIELD 。然后你可以像这样使用UUIDField

import uuid
from django.db import models
from django.contrib.auth.models import AbstractBaseUser,PermissionsMixin

class User(AbstractBaseUser,PermissionsMixin):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    # other fields

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

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