Where to include
models.OneToOneField(CustomUser, related_name='profile')

I tried including this in customuser model it does not understand.



On 5 March 2017 at 14:24, ludovic coues <[email protected]> wrote:

> You should look at the related_name argument of relationship. Using `
> models.OneToOneField(CustomUser, related_name='profile')` will add a
> profile property to your custom user object.
>
> Then your function became something like that :
>
>     def get_full_name(self):
>         full_name = '%s %s' %(self.profile.first_name, self.profile.last_
> name)
>         return full_name.strip()
>
> Don't forget to write some test and to take into account what happens if
> you user have no UserProfile associated
>
> On 5 Mar 2017 1:09 a.m., "sarfaraz ahmed" <[email protected]> wrote:
>
> I am trying to create custom user authentication using abstractbaseuser.
> All worked fine. but I wanted to move first_name and last_name to other
> models
>
>
> from django.db import models
> from django.contrib.auth.models import AbstractBaseUser,BaseUserManag
> er,PermissionsMixin
> from django.core.mail import send_mail
> from django.utils.translation import ugettext_lazy as _
> #now=time.strftime('%Y-%M-%D %H:%m:%S.%u%Z')
> import datetime
> from datetime import timedelta
>
> from django.utils import timezone
>
> tomorrow = datetime.date.today() + timedelta(days=1)
>
> class CustomUserManager(BaseUserManager):
>     def _create_user(self,email,password,is_staff,is_superuser,
> **extra_fields):
>
>         if not email:
>             raise ValueError('The given email must be set')
>
>         email=self.normalize_email(email)
>         user= self.model(email=email,
>                          is_staff=is_staff,
>                          is_active = True,
>                          is_superuser =is_superuser,
>                          last_login=timezone.now(),
>                          date_joined=timezone.now(),
>                         **extra_fields)
>         user.set_password(password)
>         user.save(using=self._db)
>         return user
>
>     def create_user(self, email,password=None,**extra_fields):
>         return self._create_user(email,passwo
> rd,False,False,**extra_fields)
>
>     def create_superuser(self, email,password,**extra_fields):
>         return self._create_user(email,password,True,True,**extra_fields)
>
> class CustomUser(AbstractBaseUser,PermissionsMixin):
>     username =models.CharField(max_length =256, unique = True,blank =
> True,null= True)
>     email =models.EmailField(blank=False, unique =True)
>     date_joined  = models.DateTimeField(_('date joined'),
> default=timezone.now())
>     is_active    = models.BooleanField(default=True)
>     is_admin     = models.BooleanField(default=False)
>     is_staff     = models.BooleanField(default=False)
>     is_superuser = models.BooleanField(default=False)
>
>     USERNAME_FIELD ='email'
>     REQUIRED_FIELD =['user_name','date_joined']
>
>     objects=CustomUserManager()
>
>     class Meta:
>         verbose_name=_('user')
>         verbose_name_plural=_('users')
>
>     def get_absolute_url(self):
>         return "/user/%s" %urlquote(self.email)
>
>     def get_full_name(self):
>
>
>
> *        #a=UserProfile.objects.get(id=id)
> #self.first_name=a.first_name        #self.last_name= a.last_name*
>         full_name = '%s %s' %(self.first_name,self.last_name)
>         return full_name.strip()
>
>     def get_short_name(self):
>         self.first_name='a'
>         return self.first_name
>
>     def email_user(self,subject,message,from_email=None):
>         send_mail(subject,message,from_email,[self.email])
>
>
>         #code
>
> class UserProfile(models.Model):
>
>     email = models.OneToOneField(CustomUser)
>     first_name=models.CharField(max_length =256, blank = True)
>     last_name=models.CharField(max_length =256, blank = True)
>     activation_key = models.CharField(max_length=40,blank=True)
>     gender = models.CharField(max_length=6, choices=(
>         ('male', 'Male'),
>         ('female', 'Female'),))
>     date_of_birth=models.DateField(null=True)
>     key_expires = models.DateTimeField(default=timezone.now())
>
>     def __str__(self):
>         full_name = '%s %s' %(self.first_name,self.last_name)
>         return full_name
>
>     class Meta:
>         verbose_name=u'User profile'
>         verbose_name_plural=u'User profiles'
>
>
> class UserAddress(models.Model):
>     address_contact=models.CharField(max_length=300,blank=False)
>     address_line1=models.CharField(max_length=300,blank=False)
>     address_line2=models.CharField(max_length=300,blank=True)
>     land_mark=models.CharField(max_length=100,blank=False)
>     city=models.CharField(max_length=140,blank=False)
>     state=models.CharField(max_length=100,blank=False)
>     pin_code = models.BigIntegerField()
>     mobile_no=models.CharField(max_length=13,blank=True)
>     last_shipped_flag=models.BooleanField(default=False)
>     is_active_flag=models.BooleanField(default=True)
>     creation_date=models.DateTimeField(auto_now_add=True,editable=False)
>     updation_date=models.DateTimeField(auto_now=True,editable=False)
>     user=models.ForeignKey(UserProfile,default=0)
>
>     def __str__(self):
>         return self.address_contact
>
>
>     class Meta:
>         verbose_name=u'User Address'
>         verbose_name_plural=u'User Addresses'
>
>
>     Now get_full_name is part of customuser model where was first_name and
> last_name are part UserProfile.
> Please help me to join both models using onetoone relationship
>
> --
> Thanks with regards,
> Sarfaraz Ahmed
>
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/ms
> gid/django-users/CAEPJdixhkp2hw1w79nMLB6EG8SCfL37hwokiy07wdi
> B969BiVA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAEPJdixhkp2hw1w79nMLB6EG8SCfL37hwokiy07wdiB969BiVA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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/CAEuG%2BTajrxiFpqjoQ3CPWZpJgkRsuxBE6
> BtM%2BbW_8jn0t5PVKg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAEuG%2BTajrxiFpqjoQ3CPWZpJgkRsuxBE6BtM%2BbW_8jn0t5PVKg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Thanks with regards,
Sarfaraz Ahmed

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/CAEPJdiz339LmEZGyrMghXWHRJ%3D-EZY_7uHWpYxTKAjd2%2BzQkSg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to