Extending the user model with Django-Registration

烂漫一生 提交于 2019-12-06 03:08:19
Adrian Lopez

PROBLEM SOLVED - SOLUTION

Have been some days but i got it! :D i found this post from shacker: Django-Registration & Django-Profile, using your own custom form

apparently in django-registration 0.8 save() method can't be override. so, the other two options are: Use signals or Rewrite the backend.

both solutions are explained in the post i linked. If someone have the same problem i'll try to help in the comments :)

NOTE: Using signals worked fine but i had some problems taking the values from the form. So i implemented the backend method and everything went ok. I strongly recomend you read shacker's post but, if you are really desesperate, theres my code:

my forms.py

class RegistrationForm(forms.Form):
    username = forms.RegexField(regex=r'^\w+$',
                            max_length=30,
                            widget=forms.TextInput(attrs=attrs_dict),
                            label=_("Username"),
                            error_messages={'invalid': _("This value must contain only letters, numbers and underscores.")})
    email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
                                                           maxlength=75)),
                         label=_("Email address"))
    password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
                            label=_("Password"))
    password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
                            label=_("Password (again)"))

    #ADITIONAL FIELDS
    #they are passed to the backend as kwargs and there they are saved
    nombre = forms.CharField(widget=forms.TextInput(attrs=attrs_dict),
                            label=_("Nombre"))

__init__.py (app/user_backend/__init__.py)

class DefaultBackend(object):
def register(self, request, **kwargs):
        username, email, password, nombre = kwargs['username'], kwargs['email'], kwargs['password1'], kwargs['nombre']
        if Site._meta.installed:
            site = Site.objects.get_current()
        else:
            site = RequestSite(request)
        new_user = RegistrationProfile.objects.create_inactive_user(username, email,
                                                                password, site)
        signals.user_registered.send(sender=self.__class__,
                                 user=new_user,
                                 request=request)

        usuario(user = User.objects.get(username=new_user.username), nombre=nombre).save()
        return new_user

root URLS.PY before this line -> url(r'profiles/', include('profiles.urls')),

    url(r'^accounts/registrar_usuario/$',
'registration.views.register',
{'backend': 'Hesperides.apps.accounts.user_regbackend.DefaultBackend','form_class':user_RegistrationForm},        
name='registration_register'
),

Thanks

mattsnider for your patience and for show me the very, very usefull pdb. Regards from spain. and: shacker and dmitko for show me the way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!