【问题标题】:first_name and last_name not saving in custom Django_Registration formfirst_name 和 last_name 未保存在自定义 Django_Registration 表单中
【发布时间】:2012-02-21 21:24:33
【问题描述】:

用户提交表单后,我无法保存要保存的 first_name 和 last_name 变量。这是我的代码。我需要帮助。

Models.py

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

class MyRegistration(models.Model):
   first_name = models.CharField(max_length = 100, null = True, unique = True)
   last_name = models.CharField(max_length = 100, null = True, unique = True)

def __unicode__(self):
  return self.name

Forms.py

from registration.forms import RegistrationForm
from django.forms import ModelForm
from models import MyRegistration
from django import forms

class MyRegistrationForm(forms.ModelForm):
  class Meta:
    model = MyRegistration


RegistrationForm.base_fields.update(MyRegistrationForm.base_fields)

class CustomRegistrationForm(RegistrationForm):
   def save(self, profile_callback = None):
     user = super(CustomRegistrationForm, self).save(profile_callback = None)
     org, c = MyRegistration.objects.get_or_create(user=user, first_name =   
     self.cleaned_data['first_name'], last_name = self.cleaned_data['last_name'])

在根 urls.py 中

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

【问题讨论】:

    标签: django django-admin django-forms django-registration


    【解决方案1】:

    这是一种非常奇怪的方法,似乎有一些交叉线。首先,如果您希望保存一些有关用户的额外数据,则不应该通过注册模型来完成 - 您应该创建一个处理额外数据的profile 应用程序。 django 文档have some good suggestions on how to go about this

    更重要的是,您更有可能为用户创建的默认 User 对象已经具有 first_namelast_name 字段,因此您可以使用它们而不是创建新的配置文件模型

    【讨论】:

      猜你喜欢
      • 2016-02-20
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      相关资源
      最近更新 更多