django-registration-redux add extra field

后端 未结 2 1243
挽巷
挽巷 2020-12-15 14:14

7 and python 2.7. i want to add extra field in django registration. i try to extend with my model like this:

class Seller(models.Model):
user            = mo         


        
2条回答
  •  無奈伤痛
    2020-12-15 15:07

    I found a way to do this! (The last answer was throwing me some errors) Let me explain it: (Django 1.10.5 and Django-registration-redux 1.4)

    app/models.py:

    class Profile(models.Model):
        user = models.OneToOneField(User, primary_key=True)
        passport_id = models.IntegerField(max_length=10)
    

    app/forms.py:

    from registration.forms import RegistrationFormUniqueEmail
    from django import forms
    from .models import Profile
    
    
    class ProfileForm(RegistrationFormUniqueEmail):
        passport = forms.IntegerField(max_length=10)
    

    Then, I made the -famous- regbackend.py file. Note that the register method takes only one parameter (the form). You need to remember to create -and save- the new object (record) in the DB's model's table aswell, as I do in the following code:

    app/regbackend.py

    from registration.backends.default.views import RegistrationView
    from .forms import ProfileForm
    from .models import Profile
    
    
    class MyRegistrationView(RegistrationView):
    
        form_class = ProfileForm
    
        def register(self, form_class):
            new_user = super(MyRegistrationView, self).register(form_class)
            p = form_class.cleaned_data['passport']
            new_profile = Profile.objects.create(user=new_user, passport=p)
            new_profile.save()
            return new_user
    

    root/urls.py

    from app.regbackend import MyRegistrationView
    
    
    urlpatterns = [
        url(r'^accounts/register/$', MyRegistrationView.as_view(),
            name='registration_register'),
        url(r'^accounts/', include('registration.backends.default.urls'))]
    

    After this, You may want to setup some other things for the field (Validations, etc.)

提交回复
热议问题