2

I have a problem using django-registration-redux.

I want to add others fields to the User as birthday, telephone, etc but it's difficult for me extends User class in models. What's the best form to add these fields to my user and show it in the form registration and login?

jonango
  • 197
  • 1
  • 9

1 Answers1

2

I would suggest to follow these steps:

  1. define your user model, adding your desird fields, e.g. birth_date and photo:

    # filename: myapp/models.py
    
    from django.db import models
    from django.utils.translation import ugettext as _
    from datetime import datetime
    from django.conf import settings
    from django.contrib.auth.models import AbstractUser, Group
    
    class MyUser(AbstractUser):
    
        birth_date = models.DateField(null=True)
        photo = models.ImageField(upload_to=..., null=True, blank=True, verbose_name=_("photo"))
    
  2. create a custom registration form:

    # in file myapp/forms.py
    from django import forms
    from registration.forms import RegistrationForm
    
    class MyRegistrationForm(RegistrationForm):
    
        birth_date = ...
        photo = ...
    
  3. write a custom registration view:

    # in file myapp/views.py
    
    from registration.backends.simple.views import RegistrationView
    from .forms import MyRegistrationForm
    
    class MyRegistrationView(RegistrationView):
    
        form_class = MyRegistrationForm
    
        def register(self, request, form):
    
            user = super(MyRegistrationView, self).register(request, form)
            user.birth_date = form.cleaned_data["birth_date"]
            user.photo = form.cleaned_data["photo"]
    
            user.save()
    
            return user
    
  4. tell the system you are going to use your custom user model

    # in file settings.py
    AUTH_USER_MODEL = "myapp.MyUser"
    
  5. add an url to invoke your custom registration view

    # in file urls.py
    
    from myapp.views import MyRegistrationView
    ...
    
    urlpatterns = [
        ...
        url(r'^accounts/register/$', MyRegistrationView.as_view(), name="registration_register"),
        ...
    ]
    
FSp
  • 1,545
  • 17
  • 37
  • I tried it but in models.py i got: ERRORS: Loguear.MyUser.groups: (fields.E304) Reverse accessor for 'MyUser.groups' clashes with reverse accessor for 'User.groups'. HINT: Add or change a related_name argument to the definition for 'MyUser.groups' or 'User.groups'. Loguear.MyUser.user_permissions: (fields.E304) Reverse accessor for 'MyUser.user_permissions' clashes with reverse accessor for 'User.user_permissions'. System check identified 4 issues (0 silenced). What would be the problem? (other similar errors appeared too) – jonango Nov 04 '15 at 01:27
  • it seems you forgot to set `AUTH_USER_MODEL` in your `settings.py`, or you did so in a settings file that you do not load. See: http://stackoverflow.com/questions/26703323/django-abstract-user-error – FSp Nov 04 '15 at 07:12
  • Thanks for reply. I was reading about this and I found that AUTH_USER_MODEL was deprecated... What are the implications in my proyect? – jonango Nov 04 '15 at 20:39
  • Can you please point me where you read so? It's new to me. BTW did you make it working? – FSp Nov 04 '15 at 22:15
  • I'm sorry for that... I was confused with auth_profile_module, AUTH_USER_MODEL is avaliable... However it doesnt work. When I try to make migrations it says "You are trying to add a non-nullable field 'password' to customuser without a default". (customuser is my model) – jonango Nov 05 '15 at 00:53
  • can you share your `customuser` definition? – FSp Nov 06 '15 at 13:16