0

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.

1 Answers1

0

You are quite right, you may have to implement the method create_user on your models.py.

Something like this:

class UserManager(BaseUserManager):
def create_user(self, email, phone_number, password=None):
    """
    Creates and saves a User with the given email
    phone_number and password.
    """
    if not email:
        raise ValueError('Informe um email por favor.')

    user = self.model(
            email=self.normalize_email(email),
            phone_number=phone_number
    )

    user.set_password(password)
    user.save(using=self._db)
    return user

def create_superuser(self, email, phone_number, password):
    """
    Creates and saves a superuser with the given email
    phone_number and password.
    """
    user = self.create_user(email,
                            password=password,
                            phone_number=phone_number
                            )
    user.is_admin = True
    user.save(using=self._db)
    return user

Hope it helped you.

Other thing you might think about is to call django signal on your view.py.

signals.user_registered.send(sender=self.__class__,
                                 user=user,
                                 request=self.request)
fefedo
  • 11
  • 4