【问题标题】:django-registration-redux add extra fielddjango-registration-redux 添加额外字段
【发布时间】:2015-06-19 16:26:42
【问题描述】:

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 出现错误语法?

谢谢

【问题讨论】:

  • 您可以扩充或替换默认的 Django django.contrib.auth.models.User 模型。查看文档:"substituting a custom User model"
  • 另外,User 模型已经有一个 email 字段和一个 get_full_name 方法,“返回 first_name 和 last_name,中间有一个空格。”

标签: python django django-registration


【解决方案1】:

这对我有用:
模型.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'),
]

【讨论】:

【解决方案2】:

我找到了一种方法! (The last answer 向我抛出了一些错误)让我解释一下:(Django 1.10.5 和 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)

然后,我制作了 - Famous- regbackend.py 文件。注意 register 方法只需要一个参数(形式)。您还需要记住在数据库模型的表中创建并保存新对象(记录),就像我在以下代码中所做的那样:

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

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

【讨论】:

  • 我对 Django 不是很熟悉,但这看起来非常类似于 18 个月大的投票答案。您可能想指出这与现有答案有何不同。
  • Dima Kudosh 的解决方案在 regbackend.py 中有问题。寄存器方法覆盖错误。让我编辑我的答案并更好地解释它
猜你喜欢
  • 1970-01-01
  • 2013-01-21
  • 2011-04-14
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 2019-12-30
  • 2018-01-19
  • 2020-01-04
相关资源
最近更新 更多