1

我被困在用户注册,我其实打算有不同的配置文件类型。注册时,我无法在创建用户时设置UserProfile。我正在使用UserCreationForm。我的文件中的代码如下。django个人资料创建,设置用户配置文件,同时使用多个配置文件类型

from django.contrib.auth.forms import UserCreationForm 
from registration.forms import RegistrationForm 
from django import forms 
from django.contrib.auth.models import User 
from accounts.models import UserProfile 
from django.utils.translation import ugettext_lazy as _ 
from person.models import Person 
from pprint import pprint 


class UserRegistrationForm(UserCreationForm): 
    #email = forms.EmailField(label = "Email") 
    fullname = forms.CharField(label = "Full name") 

    class Meta: 
     model = User 
     fields = ("email","fullname","password1","password2") 

    def __init__(self, *args, **kwargs): 
     super(UserRegistrationForm, self).__init__(*args, **kwargs) 
     del self.fields['username'] 

    def clean_email(self): 
     """ 
     Validate that the supplied email address is unique for the 
     site. 

     """ 
     if User.objects.filter(email__iexact=self.cleaned_data['email']): 
      raise forms.ValidationError(_("This email address is already in use. Please supply a different email address.")) 
     return self.cleaned_data['email'] 

    def save(self, commit=True): 
     user = super(UserRegistrationForm, self).save(commit=False) 
     #user_profile=user.set_profile(profile_type="Person") 

     UserProfile.profile.person.full_name = self.cleaned_data["fullname"] 
     user.email = self.cleaned_data["email"] 
     if commit: 
      user.save() 
     return user 

class CompanyRegistrationForm(UserCreationForm): 
    email=forms.EmailField(label="Email") 

class UserProfileForm(forms.ModelForm): 
    class Meta: 
     model=UserProfile 
     exclude=('user',) 

账户/ models.py

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


class UserProfile(models.Model): 
    user=models.OneToOneField(User) 
    meta_keywords=models.CharField("Meta Keywords",max_length=255, 
      help_text="Comma delimited set of keywords of meta tag") 
    meta_description=models.CharField("Meta Description",max_length=255, 
      help_text='Content for description meta tag') 

    def __unicode__(self): 
     return "User Profile for: "+self.username 

    class Meta: 
     ordering=['-id'] 

views.py

from django.contrib.auth.forms import UserCreationForm 
from django.template import RequestContext 
from django.shortcuts import render_to_response,get_object_or_404 
from django.core import urlresolvers 
from django.http import HttpResponseRedirect 
from django.contrib.auth.decorators import login_required 
from accounts.forms import UserRegistrationForm, UserProfileForm 
#from accounts.forms import UserProfile 

def register(request,template_name="account/register.html"): 
    if request.method=='POST': 
     postdata=request.POST.copy() 
     form=UserRegistrationForm(postdata) 
     user_profile=UserProfileForm(postdata) 
     if form.is_valid(): 
      form.save() 
      un=postdata.get('username','') 
      pw=postdata.get('password','') 
      from django.contrib.auth import login,authenticate 
      new_user=authenticate(username=un,password=pw) 
      if new_user and new_user.is_active: 
       login(request,new_user) 
       url=urlresolvers.reverse('dashboard') 
       return HttpResponseRedirect(url)  
    else: 
     form=UserRegistrationForm() 
    page_title="User Registration" 
    return render_to_response(template_name,locals(),context_instance=RequestContext(request)) 


@login_required 
def dashboard(request): 
    pass 

@login_required 
def settings(request): 
    pass 

正如我使用多个配置文件,以便以下是这些配置文件models.py中的一个的代码:

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

class Person(UserProfile): 
    skills=models.CharField(max_length=100) 
    fullname=models.CharField(max_length=50) 
    short_description=models.CharField(max_length=255) 
    is_online=models.BooleanField(default=False) 
    tags=models.CharField(max_length=50) 
    profile_pic=models.ImageField(upload_to="person_profile_images/") 
    profile_url=models.URLField() 
    date_of_birth=models.DateField() 
    is_student=models.BooleanField(default=False) 
    current_designation=models.CharField(max_length=50) 
    is_active_jobseeker=models.BooleanField(default=True) 
    current_education=models.BooleanField(default=True) 


    class Meta: 
     db_table='person' 

我的资料auth i ñsettings.py

AUTH_PROFILE_MODULE='accounts.UserProfile' 

这里是也是我之后看一些其他地方,profile.py使用的文件: 从accounts.models导入用户配置 从accounts.forms进口量从person.models进口UserProfileForm 人 从company.models进口公司

def retrieve(request,profile_type): 
    try: 
     profile=request.user.get_profile() 
    except UserProfile.DoesNotExist: 
     if profile_type=='Person': 
      profile=Person.objects.create(user=request.user) 
     else: 
      profile=Company.objects.create(user=request.user) 
     profile.save() 
    return profile 

def set(request,profile_type): 
    profile=retrieve(request,profile_type) 
    profile_form=UserProfileForm(request.POST,instance=profile) 
    profile_form.save() 

我是新和迷惑,看到的文档也。也看到了stackoverflow.com的其他解决方案,但没有找到我的问题的任何解决方案。所以请告诉你是否找到对我有用的东西。这似乎不是一个大问题,但由于我对它很陌生,所以对我来说这是一个问题。

回答

2

多个配置文件类型不能用于Django配置文件机制所需的OneToOne关系。我建议您保留一个包含所有配置文件类型通用数据的配置文件类,并将类型特定的数据存储在单独的一组类中,并使用generic relation链接到您的配置文件类。

编辑:

感谢您的澄清。今天再看看你的代码,似乎你可能确实能够完成你对模型继承的尝试。我认为这个问题出现在UserRegistrationFormsave()方法中。尝试这样的:

def save(self, commit=True): 
    user = super(UserRegistrationForm, self).save(commit=False) 
    user.email = self.cleaned_data["email"] 
    if commit: 
     user.save() 
     person = Person(user=user) 
     person.full_name = self.cleaned_data["fullname"] 
     person.save() 
    return user 
+0

如果我用户Foreignkey与唯一= True?我将如何连接它呢?因为模型明智,我已经完成了研发工作,所以目前我在连接配置文件时遇到了问题,而用户的注册是我之前没有做过的。所以我不知道如何连接,无论是父类是USerProfile和2个不同的人和公司继承。 – Hafiz

+0

所以'Person(user = user)'将连接到Person,并更新'UserProfile'和'Person'表中的数据?那么Django会通过这个陈述理解所有这些关系吗?对不起愚蠢的问题,但我是新的,所以只是想了解,真的很感谢你的时间再次看它 – Hafiz

+0

我认为它应该工作。虽然没有测试过。你试过了吗?任何问题特别是? –

相关问题