Reputation: 75
What i wanted to achieve? I wanted to achieve {Company Letter}- {Country Code}-{1st Letter of 1st Name}{1st Letter of Last Name}-{000001-999999} For example: Company Name: Stack Overflow Username: Stack Overflow Country is USA SO - 001 - SO - 000001
The Registration numbers will start from ONE when i choose different countries.. For example USA the registration number will start from (000001-999999), for UK (000001-999999) and so on...
For that i have created a country, state and city model. I have created a custom user model. In the custom user model, i am using country, state, city drop down.
So there is one field called registration numbers.. How can i generate registration number after registering one user..
Code:
from django.utils.translation import ugettext_lazy as _
from smart_selects.db_fields import ChainedForeignKey
class Country(models.Model):
name = models.CharField(_('Country'), max_length=255, null=True)
country_code = models.CharField(_('Code'), max_length=10, null=True)
iso_code_short = models.CharField(_('ISO Code Short'), max_length=2, null=True)
iso_code_long = models.CharField(_('ISO Code Long'), max_length=3, null=True)
def __str__(self):
return self.name
class State(models.Model):
country = models.ForeignKey(Country, on_delete=models.CASCADE)
name = models.CharField(_('State'), max_length=255, null=True)
def __str__(self):
return str(self.name)
class City(models.Model):
country = models.ForeignKey(Country, on_delete=models.CASCADE)
state = ChainedForeignKey('State', chained_field="country", chained_model_field="country", show_all=False, auto_choose=True)
name = models.CharField(_('City'), max_length=255)
def __str__(self):
return str(self.name)
Custom User Model
class CustomUser(AbstractUser):
username = None
email = models.EmailField(_('Email'), max_length=255, unique=True)
first_name = models.CharField(_('First Name'), max_length=100)
last_name = models.CharField(_('Last Name'), max_length=100)
registration_no = models.CharField(_('Registration Number'), max_length=50, null=True, blank=True)
country = models.ForeignKey(Country, on_delete=models.SET_NULL)
state = ChainedForeignKey(State, chained_field="country", chained_model_field="country", show_all=False, auto_choose=True)
city = ChainedForeignKey(City, chained_field="state", chained_model_field="state", show_all=False, auto_choose=True)
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
Please guide me..
Upvotes: 0
Views: 600
Reputation: 1153
It's a very simple answer: continue using Django and Python.
You just need a concatenation constructions, something like:
f"{company.letter}-{country.code}..."
etc.
So create all your models with proper fields and methods, then create the registration_number
field to store a new registration id and create the value for it on instance's .save()
method.
Upvotes: 1