I am fairly new to django and this might be a basic level question for someone experienced but any help will be greatly appreciated.
I am trying to create a custom registration form for the user using django-registration-redux package. I have the following implementation as of now:
models.py:
from django.db import models
from django.contrib.auth.models import User
class MyUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
date_of_birth = models.DateField()
views.py:
class UserProfileRegistration(RegistrationView):
success_url = '/'
form_class = UserProfileRegistrationForm
def register(self, form):
"""
Implement user-registration logic here.
"""
User = MyUser
user = User.objects.create_user(
username = form.cleaned_data['username'],
first_name = form.cleaned_data['first_name'],
last_name = form.cleaned_data['last_name'],
email=form.cleaned_data['email'],
password=form.cleaned_data['password1']
)
if not self.registration_allowed():
return redirect(self.disallowed_url)
return redirect(self.get_success_url(user))
forms.py:
from registration.forms import RegistrationForm
from django import forms
from django.forms.extras import SelectDateWidget
import datetime
from .models import MyUser
class UserProfileRegistrationForm(RegistrationForm):
first_name = forms.CharField(max_length=15, label='First name')
last_name = forms.CharField(max_length=15, label='Last name')
date_of_birth = forms.DateField(label='Date of birth',
widget=SelectDateWidget(years=[y for y in range(1950,
datetime.datetime.now().year-17)],
attrs=({'style': 'width: 20%; display: inline-block;'})),)
class Meta:
model = MyUser
fields = ("email", 'first_name', 'last_name',
'date_of_birth')
urls.py
from .views import UserProfileRegistration
urlpatterns = [
url(r'^accounts/register/$', UserProfileRegistration.as_view(), name='registration_register'),
url(r'^accounts/', include('registration.backends.simple.urls')),
]
Firstly, this method does not output a username field in the registration form and I am not sure how to achieve that. In fact, I would like to make the e-mail USERNAME_FIELD for the MyUser model. In order to do that where exactly in the workflow I have to declare it?
Most importantly, when I runserver
with the above code, it throws me the following exception:
'Manager' object has no attribute 'create_user'
Perhaps, I have to inherit from the AbstractBaseUser class to allow the manager to access the create_user method. I am not very certain if that is the only choice I have. Is there anyway I can extend the django legacy user authentication to accomplish the above instead of implementing a custom auth? Thanks in advance.