包括django userena配置文件中的额外字段

时间:2014-04-26 09:18:28

标签: django django-userena

我在myprofile模型中添加了一个额外的字段,如下所示..

import uuid
import base64
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.db.models.signals import post_save
from userena.models import UserenaBaseProfile
from userena.utils import user_model_label 

class MyProfile(UserenaBaseProfile):  
    user = models.OneToOneField(User,unique=True,  
                          verbose_name=_('user'),related_name='my_profile')  
    favourite_snack = models.CharField(_('favourite snack'),max_length=5)
    referral_code = models.CharField(max_length=300, blank=True, null=True)

    def get_absolute_url(self):
        return u'/profile/show/%d' % self.id
    def generate_verification_code(self):
        return base64.urlsafe_b64encode(uuid.uuid1().bytes.encode("base64").rstrip())[:25]
    def save(self, *args, **kwargs):
        if not self.pk:
            self.referral_code = self.generate_verification_code()
        elif not self.verification_code:
            self.referral_code = self.generate_verification_code()
        return super(MyProfile, self).save(*args, **kwargs)

现在注册后,引用代码正在生成,那没关系。但是当我尝试使用userena配置文件编辑选项编辑我的个人资料时,我正面临着一个错误...

AttributeError at /accounts/veer/edit/    
'MyProfile' object has no attribute 'verification_code'

是这个错误提升,因为我在我的Myprofile模型中添加了一个新字段(引用代码)?如果是,那么我应该怎么做用userena调整一个新字段(如 referral_code )?

1 个答案:

答案 0 :(得分:0)

只是猜测,您可以将referral_code作为属性字段。而不是modelField

class MyProfile(UserenaBaseProfile):  
      user = models.OneToOneField(User,unique=True,  
                      verbose_name=_('user'),related_name='my_profile')  
      favourite_snack = models.CharField(_('favourite snack'),max_length=5)

      def get_absolute_url(self):
         return u'/profile/show/%d' % self.id
      def generate_verification_code(self):
         return base64.urlsafe_b64encode(uuid.uuid1().bytes.encode("base64").rstrip())[:25]
      def save(self, *args, **kwargs):
         if not self.pk:
            referral_code = property(generate_verification_code)
         elif not self.verification_code:
            referral_code = property(generate_verification_code)
         return super(MyProfile, self).save(*args, **kwargs)

在您的管理员中加入referral_code字段。您可以使用print (self.instance.referral_code)

进行检查