Reputation: 5
This is my models.py file
from django.db import models
from django.contrib.auth.models import AbstractUser
from .manager import UserManager
# Create your models here.
class User(AbstractUser):
account_id = models.IntegerField(unique=True, default=1111111)
log_id = models.IntegerField(unique=True, default=0000000)
username = None
email = models.EmailField(unique=True)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
From this code, I am able to make an account id from the admin panel but I want to generate a unique account id automatically when users fill out the registration form it should generate a unique account id in the admin panel.
This is my tools.py file
from random import randint
from .models import *
from django.contrib.auth import get_user_model
User = get_user_model()
def User():
def account_id():
account_id = randint(1000000, 9999999)
print(account_id)
is_unique = User.objects.filter(account_id=account_id).exists()
if not User.account_id:
is_unique = False
while not is_unique:
account_id
User.account_id = account_id
User.save()
else:
account_id()
User.save()
```
Upvotes: 0
Views: 455
Reputation: 2623
Common practice is to use a UUID field on the model like e.g.
import uuid
class User(AbstractUser):
account_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
...
Note that most of the times it might be sufficient to just use the default ID of the model to distinguish between instances.
Upvotes: 1