Last active
November 6, 2017 15:51
-
-
Save rhenter/16d5fd984499c57bd8278e33a1b1fea6 to your computer and use it in GitHub Desktop.
Django Custom User
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from django.contrib.auth.forms import UserCreationForm | |
class FormSignUp(UserCreationForm): | |
email = forms.CharField(max_length=254, required=True, widget=forms.EmailInput()) | |
class Meta: | |
model = User | |
fields = [ | |
'first_name', 'last_name', 'birthdate', 'gender', 'site_facebook', 'site_twitter', | |
'profile_picture', 'username', 'email', 'password1', 'password2' | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from dateutil.relativedelta import relativedelta | |
from django.db import models | |
from django.contrib.auth.models import AbstractUser | |
GENDERS = ( | |
('m', 'Male'), | |
('f', 'Female'), | |
) | |
class User(AbstractUser): | |
location = models.CharField(max_length=30, blank=True) | |
birthdate = models.DateField(null=True, blank=True) | |
gender = models.CharField(max_length=64, choices=GENDERS) | |
site_facebook = models.URLField(blank=True) | |
site_twitter = models.URLField(blank=True) | |
profile_picture = models.ImageField(upload_to='profile_pictures', blank=True) | |
@property | |
def age(self): | |
today = datetime.date.today() | |
if self.birthdate: | |
return u"%s" % relativedelta.relativedelta(today, self.birthdate).years | |
else: | |
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core.urlresolvers import reverse_lazy | |
from django.views.generic import CreateView | |
from .forms import FormSignUp | |
class CreateUser(CreateView): | |
form_class = FormSignUp | |
template_name = 'signup.html' | |
success_url = reverse_lazy('home') | |
def form_valid(self, form): | |
with transaction.atomic(): | |
form.instance.created_by = self.request.user | |
form.instance.updated_by = self.request.user | |
self.object = form.save() | |
return super(CreateUser, self).form_valid(form) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment