5

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            = models.ForeignKey(User, unique=True)
name            = models.CharField(max_length=25)
phone_number    = models.BigIntegerField()
email           = models.EmailField(max_length=75)

def __str__(self):
    return self.name;

and i add form.py like this

    from django import forms
    from registration.forms import RegistrationForm
    from django.forms import ModelForm
    from django.contrib.auth.models import User
    from kerajinan.models import Product, Category, Seller

    class SellerForm(forms.ModelsForm):
        class Meta:
            model   = Seller
            fields  = ('name','phone_number','email')

and modify url.py like this:

url(r'^accounts/', 'registration.views.register',{'form_class':SellerForm,'backend': 'registration.backends.default.DefaultBackend'})

how to use those model with django registration and i get error syntax with my ulr.py?

thanks

User0511
  • 665
  • 3
  • 11
  • 27
  • You can augment or replace the default Django `django.contrib.auth.models.User` model. Look into the documentation: ["substituting a custom User model"](https://docs.djangoproject.com/en/1.8/topics/auth/customizing/) – Timmy O'Mahony Apr 14 '15 at 07:20
  • Also, the User model already has an [email](https://docs.djangoproject.com/en/1.5/ref/contrib/auth/#django.contrib.auth.models.User.email) field, and a [get_full_name](https://docs.djangoproject.com/en/1.5/ref/contrib/auth/#django.contrib.auth.models.User.get_full_name) method that "Returns the first_name plus the last_name, with a space in between." – Mick T Aug 26 '15 at 20:37

2 Answers2

7

It's works for me:
models.py

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    field = models.CharField(max_length=3)
    user = models.OneToOneField(User)

forms.py

from registration.forms import RegistrationFormUniqueEmail
from django import forms

class UserProfileRegistrationForm(RegistrationFormUniqueEmail):
    field = forms.CharField()

Create regbackend.py and write:

from registration.backends.default.views import RegistrationView
from forms import UserProfileRegistrationForm
from models import UserProfile

class MyRegistrationView(RegistrationView):

    form_class = UserProfileRegistrationForm

    def register(self, request, form_class):
        new_user = super(MyRegistrationView, self).register(request, form_class)
        user_profile = UserProfile()
        user_profile.user = new_user
        user_profile.field = form_class.cleaned_data['field']
        user_profile.save()
        return user_profile

And urls.py

from django.conf.urls import include, url
import regbackend

urlpatterns = [
url(r'^accounts/register/$', regbackend.MyRegistrationView.as_view(), name='registration_register'),
]
Dima Kudosh
  • 7,126
  • 4
  • 36
  • 46
  • I was getting an error using your code directly. I got it to work by returning the `new_user` (instead of `user_profile`) class from `register` function. – sudshekhar Aug 15 '15 at 18:26
  • The Django docs suggest using a OneToOneField. See: https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#extending-the-existing-user-model – Mick T Aug 26 '15 at 20:34
4

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.)

revliscano
  • 2,227
  • 2
  • 12
  • 21
  • 1
    I'm not very familiar with Django, but this looks **very** similar to the 18 month old upvoted answer. You may want to point out how this differs from the existing answer. – miken32 Jan 25 '17 at 22:19
  • 1
    The Dima Kudosh's solution has problems in regbackend.py. The register method overriding was wrong. Let me edit my answer and explain it better – revliscano Jan 25 '17 at 23:51