django-userena的多个用户配置文件 [英] Multiple User Profiles in django-userena

查看:100
本文介绍了django-userena的多个用户配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Userena制作多个配置文件 - 每个用户只能有一个配置文件类型。在其他讨论之后,我使用多表继承,但是当我这样做时,我不能将数据保存到派生模型中。一切都成为CommonProfile,这是不可能/允许的。示例代码如下:

I am trying to make multiple profile for Userena - each User can only have one Profile type. Following other discussions, I am using multiple table inhertience, but when I do this, I cannot get data to be saved into the derived models. Everything ends up being CommonProfile, which should not be possible/permitted. Example code follows:

in models.py

# models.py
# This is an example of how i've attempted to get 
# multiple user profiles working with Userena
from django.contrib.auth.models import User
from django.db import models
from userena.models import UserenaLanguageBaseProfile

class CommonProfile(UserenaLanguageBaseProfile):
    """Common fields for 2 user profiles: Spam and Eggs"""
    user = models.OneToOneField(User)
    common_field = models.CharField(max_length=100)

    @property
    def is_spam(self):
        """Find out if this is a Spam user"""
        try:
            self.spamprofile
            return True
        except SpamProfile.DoesNotExist:
            return False

    def get_real_type(self):
        """return the real model"""
        if self.is_spam:
            return self.spamprofile
        else:
            return self.eggsprofile

class SpamProfile(CommonProfile):
    spam_field = models.CharField(max_length=20)

class EggsField(CommonProfile):
    eggs_field = models.SmallIntegerField()

表单.py

# forms.py
# This is the form to sign up a Spam Type Person

from django import forms
from userena.forms import SignupFormTos
from . models import CommonProfile, SpamProfile

class SpamSignupForm(SignupFormTos):
    """signup a Spam Person"""
    common_field = forms.CharField(label='what is your quest')
    spam_field = forms.CharField(label='what kind of spam are you')

    def clean(self):
        cleaned_data = super(SpamSignupForm,self).clean()
        #do stuf, same idea for clean_<field>
        return cleaned_data

    def save(self):
        """Save the SpamProfile"""
        user = super(SpamSignupForm,self).save()
        common_profile = user.get_profile()
        spam_profile = SpamProfile(commonprofile_ptr=common_profile)
        spam_profile.spam_field = self.cleaned_data['spam_field']
        spam_profile.save()
        return spam_profile


推荐答案

这个周末我也有同样的问题。尝试保存功能

I had the same problem this weekend. Try in your save function

def save:
  #your other code
   spam_field = self.cleaned_data['spam_field']
   new_spam_user = SpamProfile.objects.create_user(spam_field)

   return new_user

这篇关于django-userena的多个用户配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆