Created
January 8, 2019 15:09
-
-
Save rg3915/0b97308cf0123ac73b58a8bd1b584c59 to your computer and use it in GitHub Desktop.
Django: Save username equal email - salvar username igual email
This file contains hidden or 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 AuthenticationForm, UserCreationForm | |
from django.contrib.auth.models import User | |
class LoginForm(AuthenticationForm): | |
username = forms.EmailField(label='Email') | |
class Meta: | |
model = User | |
class UserAdminCreationForm(UserCreationForm): | |
''' Cadastro geral de User ''' | |
first_name = forms.CharField(label='Nome') | |
last_name = forms.CharField(label='Sobrenome') | |
email = forms.CharField(label='E-mail') | |
cpf = forms.CharField(label='CPF') # UserProfile | |
class Meta: | |
model = User | |
fields = ('first_name', 'last_name', 'email', 'cpf') | |
def save(self, commit=True): | |
instance = super(UserAdminCreationForm, self).save(commit=False) | |
# Salva username = email | |
instance.username = self.cleaned_data['email'] | |
if commit: | |
instance.save() | |
return instance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment