Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

extending the existing user model

# models.py
class User(AbstractUser):
    username = None
    email = models.EmailField(unique=True, verbose_name=_("email"))
    phone = models.CharField(unique=True, max_length=11, verbose_name=_("phone"))
    ...
    USERNAME_FIELD = "email"
    EMAIL_FIELD = "email"
    ...

class Employee(models.Model):
    ...
    user = models.OneToOneField(
        "user.User",
        on_delete=models.CASCADE,
        related_name="employee",
        verbose_name=_("user"),
    )

class Company(models.Model):
    ...

class Member(models.Model):
    is_owner = models.BooleanField(default=False, verbose_name=_("owner"))
    is_manager = models.BooleanField(default=False, verbose_name=_("manager"))
    is_active = models.BooleanField(default=True)
    user = models.OneToOneField(
        "user.User",
        on_delete=models.CASCADE,
        related_name="member",
        verbose_name=_("user"),
    )
    company = models.ForeignKey(
        "user.Company",
        on_delete=models.CASCADE,
        related_name="members",
        verbose_name=_("company"),
    )
Comment

extending User Model

from __future__ import unicode_literals

from django.db import models
from django.core.mail import send_mail
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.base_user import AbstractBaseUser
from django.utils.translation import ugettext_lazy as _

from .managers import UserManager


class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), unique=True)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)
    is_active = models.BooleanField(_('active'), default=True)
    avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def get_full_name(self):
        '''
        Returns the first_name plus the last_name, with a space in between.
        '''
        full_name = '%s %s' % (self.first_name, self.last_name)
        return full_name.strip()

    def get_short_name(self):
        '''
        Returns the short name for the user.
        '''
        return self.first_name

    def email_user(self, subject, message, from_email=None, **kwargs):
        '''
        Sends an email to this User.
        '''
        send_mail(subject, message, from_email, [self.email], **kwargs)
Comment

PREVIOUS NEXT
Code Example
Python :: Read a string with digits from the input and convert each number to an integer. Create a list in which you should include only odd digits. 
Python :: how to check if the update_one success in flask 
Python :: how to choose appropriate graph for your dataset visualization 
Python :: ansible custom module 
Python :: python lambda append to list and return it 
Python :: allow django imagefield accept base 64 image 
Python :: read the entire images in the dataset 
Python :: django wsgi application could not be loaded error importing module 
Python :: ERROR: Target path exists but is not a directory, will not continue. 
Python :: Fill NaN with the first valid value starting from the rightmost column, then extract first column 
Python :: insert key in binary tree recursively 
Python :: How to assign a value to a dictionary if I need to reference it in the right hand side? 
Python :: Creaing your own functions 
Python :: how to give order in boxplot matplotlib 
Python :: python script to recursively scan subdirectories 
Python :: change the surface color rhinopython 
Python :: create a python file and import it as library in other file 
Python :: my name is raghuveer 
Python :: access nested set with array params python 
Python :: matplotlib x tlabels ax.set_xlabel 
Python :: raspberry pi run a python script using ssh 
Python :: pltoly boxlpot 
Python :: openpyxl add_filter column 
Python :: Return an RDD created by coalescing all elements within each partition into a list. 
Python :: django voice lib 
Python :: openpyxl _cells_by_row 
Python :: Return the indices of the bins 
Python :: how to make commas appear in integers in terminal python 
Python :: python pool 
Python :: Recursive Folder scan 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =