django-registration-redux添加额外字段

时间:2015-04-14 06:52:46

标签: python django django-registration

7和python 2.7。我想在django注册中添加额外的字段。我尝试用这样的模型扩展:

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;

我添加了像这样的form.py

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

并修改url.py,如下所示:

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

如何使用django注册的那些模型,我的ulr.py得到错误语法?

感谢

2 个答案:

答案 0 :(得分:7)

这对我有用:
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()

创建regbackend.py并写:

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

和urls.py

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

urlpatterns = [
url(r'^accounts/register/$', regbackend.MyRegistrationView.as_view(), name='registration_register'),
]

答案 1 :(得分:4)

我找到了办法! (The last answer给我一些错误)让我解释一下:(Django 1.10.5和Django-registration-redux 1.4)

应用/ models.py:

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

应用/ forms.py:

from registration.forms import RegistrationFormUniqueEmail
from django import forms
from .models import Profile


class ProfileForm(RegistrationFormUniqueEmail):
    passport = forms.IntegerField(max_length=10)

然后,我创建了-famous-regbackend.py文件。请注意,寄存器方法仅采用一个参数(表单)。你需要记住在DB的模型表中创建 - 并保存 - 新对象(记录),就像我在下面的代码中所做的那样:

应用/ 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

<强>根/ 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'))]

在此之后,您可能需要为该字段设置其他一些内容(验证等)