from django.contrib.auth.models import AbstractUserfrom django.core.validators import (MinValueValidator,MaxValueValidator,RegexValidator)from django.db import modelsfrom django.utils.translation import ugettext_lazy as _from .managers import UserManagerclass User(AbstractUser):username = models.CharField(_('username'), max_length=30, unique=True, null=True, blank=True,help_text=_('Required. 30 characters or fewer. Letters, digits and ''@/./+/-/_ only.'),validators=[RegexValidator(r'^[\w.@+-]+$',_('Enter a valid username. ''This value may contain only letters, numbers ''and @/./+/-/_ characters.'), 'invalid'),],error_messages={'unique': _("A user with that username already exists."),})email = models.EmailField(unique=True, null=False, blank=False)contact_no = models.IntegerField(unique=True, blank=True, null=True)objects = UserManager()USERNAME_FIELD = 'email'REQUIRED_FIELDS = []def __str__(self):return self.email@propertydef account_no(self):if hasattr(self, 'account'):return self.account.account_noreturn None@propertydef full_name(self):return '{} {}'.format(self.first_name, self.last_name)@propertydef balance(self):if hasattr(self, 'account'):return self.account.balancereturn None@propertydef full_address(self):if hasattr(self, 'address'):return '{}, {}-{}, {}'.format(self.address.street_address,self.address.city,self.address.postal_code,self.address.country,)return Noneclass AccountDetails(models.Model):GENDER_CHOICE = (("M", "Male"),("F", "Female"),)user = models.OneToOneField(User,related_name='account',on_delete=models.CASCADE,)account_no = models.PositiveIntegerField(unique=True,validators=[MinValueValidator(10000000),MaxValueValidator(99999999)])gender = models.CharField(max_length=1, choices=GENDER_CHOICE)birth_date = models.DateField(null=True, blank=True)balance = models.DecimalField(default=0,max_digits=12,decimal_places=2)picture = models.ImageField(null=True,blank=True,upload_to='account_pictures/',)def __str__(self):return str(self.account_no)class UserAddress(models.Model):user = models.OneToOneField(User,related_name='address',on_delete=models.CASCADE,)street_address = models.CharField(max_length=512)city = models.CharField(max_length=256)postal_code = models.PositiveSmallIntegerField()country = models.CharField(max_length=256)def __str__(self):return self.user.email
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2a43175d-5279-401a-a97d-6b1036f1ca52%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment