पहले, django documentation
एक बार का उपयोग कर एक कस्टम उपयोगकर्ता बनाने के लिए, आप अपने कस्टम उपयोगकर्ता, MyUser कक्षा के लिए जाना है। First_name, last_name या जो कुछ भी आपको चाहिए, जैसे फ़ील्ड जोड़ें।
आपको इस तरह की कुछ चीज़ मिल जाएगी। इस के लिए अपने वर्ग के भीतर
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
first_name = models.CharField(max_length=32)
last_name = models.CharField(max_length=32)
date_of_birth = models.DateField()
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['date_of_birth']
def get_full_name(self):
# The user is identified by their email address
return self.email
def get_short_name(self):
# The user is identified by their email address
return self.email
def __str__(self): # __unicode__ on Python 2
return self.email
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
अब, संपादित __str__ कार्य करते हैं।
def __str__(self): # __unicode__ on Python 2
return self.first_name
याद रखें, date_of_birth आदि जैसे फ़ील्ड वैकल्पिक हैं।
संपादन पुस्तकालय एक उचित समाधान नहीं है। आपको उपयोगकर्ता मॉडल को उप-वर्ग करना चाहिए और बाल वर्ग के भीतर स्ट्र फ़ंक्शन का उपयोग करना चाहिए। – ruddra