Skip to content

Instantly share code, notes, and snippets.

@rg3915
Created January 8, 2019 15:09
Show Gist options
  • Save rg3915/0b97308cf0123ac73b58a8bd1b584c59 to your computer and use it in GitHub Desktop.
Save rg3915/0b97308cf0123ac73b58a8bd1b584c59 to your computer and use it in GitHub Desktop.
Django: Save username equal email - salvar username igual email
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