3

我试图通过扩展 django-registration 应用程序并使用 Django Profile 来创建注册表单。我已经为配置文件创建了模型和表单,当我通过 django shell 检查时,它正在生成字段。对于配置文件字段,我使用的是 ModelForm。现在我对如何将 django-registration 和 profile 字段结合在一起感到震惊。以下是我开发的代码

模型.py

class UserProfile(models.Model):
    """
    This class would define the extra fields that is required for a user who will be registring to the site. This model will
    be used for the Django Profile Application
    """
    GENDER_CHOICES = ( 
        ('M', 'Male'),
        ('F', 'Female'),
    )

    #Links to the user model and will have one to one relationship
    user = models.OneToOneField(User)    

    #Other fields thats required for the registration
    first_name = models.CharField(_('First Name'), max_length = 50, null = False)    
    last_field = models.CharField(_('Last Name'),max_length = 50)
    gender = models.CharField(_('Gender'), max_length = 1, choices=GENDER_CHOICES, null = False)    
    dob = models.DateField(_('Date of Birth'), null = False)    
    country = models.OneToOneField(Country)
    user_type = models.OneToOneField(UserType)
    address1 = models.CharField(_('Street Address Line 1'), max_length = 250, null = False)
    address2 = models.CharField(_('Street Address Line 2'), max_length = 250)
    city = models.CharField(_('City'), max_length = 100, null = False)
    state = models.CharField(_('State/Province'), max_length = 250, null = False)
    pincode = models.CharField(_('Pincode'), max_length = 15)
    created_on = models.DateTimeField()
    updated_on = models.DateTimeField(auto_now=True)

表格.py

class UserRegistrationForm(RegistrationForm, ModelForm):

    #resolves the metaclass conflict
    __metaclass__ = classmaker()

    class Meta:
        model = UserProfile
        fields = ('first_name', 'last_field', 'gender', 'dob', 'country', 'user_type', 'address1', 'address2', 'city', 'state', 'pincode')

现在我应该怎么做才能将 django-registration 应用程序与我的自定义应用程序混合。我浏览了很多网站和链接来弄清楚它,包括Django-Registration 和 Django-Profile,使用您自己的自定义表单,但我不确定是否继续前进,特别是因为我使用的是 ModelForm。

更新(2011 年 9 月 26 日)

我按照下面@VascoP 的建议进行了更改。我更新了模板文件,然后从我的 view.py 我创建了以下代码

def register(request):
    if request.method == 'POST':        
        form = UserRegistrationForm(request.POST)
        if form.is_valid():
            UserRegistrationForm.save()
    else:
        form = UserRegistrationForm()
    return render_to_response('registration/registration_form.html',{'form' : form}, context_instance=RequestContext(request))

进行以下更改后,表单已正确呈现,但问题是数据未保存。请帮我。

更新(2011 年 9 月 27 日)

UserRegistrationForm.save() 更改为 form.save()。更新后的代码是views.py如下

def register(request):
    if request.method == 'POST':        
        form = UserRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = UserRegistrationForm()
    return render_to_response('registration/registration_form.html',{'form' : form}, context_instance=RequestContext(request))

即使在更新之后,用户也没有得到保存。相反,我收到了一个错误

“超级”对象没有“保存”属性

我可以看到 RegistrationForm 类中没有保存方法。那么我现在应该怎么做才能保存数据呢?请帮忙

4

1 回答 1

1

您知道 User 模型已经有 first_name 和 last_name 字段,对吧?此外,您将 last_name 输错了 last_field。

我建议扩展 django-registration 提供的表格并制作一个新表格来添加您的新字段。您还可以将名字和姓氏直接保存到用户模型中。

#get the form from django-registration
from registration.forms import RegistrationForm

class MyRegistrationForm(RegistrationForm):
    first_name = models.CharField(max_length = 50, label=u'First Name')    
    last_field = models.CharField(max_length = 50, label=u'Last Name')
    ...
    pincode = models.CharField(max_length = 15, label=u'Pincode')


    def save(self, *args, **kwargs):
        new_user = super(MyRegistrationForm, self).save(*args, **kwargs)

        #put them on the User model instead of the profile and save the user
        new_user.first_name = self.cleaned_data['first_name']
        new_user.last_name = self.cleaned_data['last_name']
        new_user.save()

        #get the profile fields information
        gender = self.cleaned_data['gender']
        ...
        pincode = self.cleaned_data['pincode']

        #create a new profile for this user with his information
        UserProfile(user = new_user, gender = gender, ..., pincode = pincode).save()

        #return the User model
        return new_user
于 2011-09-16T20:08:05.920 回答