(Django)对象是不可取消的

时间:2010-07-09 19:25:21

标签: python django object

当我尝试创建扩展用户配置文件时,我得到的UserProfile对象是不可取消的。我已经google了解决方案,但'你的对象不是一个序列'在这里没有多大帮助。这是我正在使用的功能,'temp_data'是我注册表中的数据:

def create_user(request):    
    data = request.session['temp_data']
    email = data['email']
    password1 = data['password1']
    userdata = {'email': email, 'password1': password1}
    backend = request.session['backend']
    #create User
    user = backend.register(request, userdata)
    data = UserProfile(user=user)
    data.is_active = False
    data.first_name = data['first_name']
    data.last_name = data['last_name']
    (... rest of the fields ...)
    data.save()

我的扩展模型:

class UserProfile(InheritedProfile):
    def upload_path(self, field_attname):
        filename = hashlib.md5(field_attname).hexdigest()[:4] + "_" + field_attname
        return settings.MEDIA_ROOT + "/uploads/users/%s" % (filename,)

    user = models.ForeignKey(User, unique=True, related_name='profile',)
    first_name = models.CharField(_("Name"), max_length=50, blank=False,)
    last_name = models.CharField(_("Surname"), max_length=50, blank=False,)
    street = models.CharField(_("Street"), max_length=50, blank=False,)
    code = models.CharField(_("Zip code"), max_length=6, blank=False,)
    city = models.CharField(_("City"), max_length=50, blank=False,) 
    image = models.ImageField(_("Avatar"), upload_to=upload_path, blank=True,)

跟踪:

File "/home/rails/site-packages/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/home/rails/registration/views.py" in register_new
  115.     data.first_name = data['first_name']

1 个答案:

答案 0 :(得分:3)

data = UserProfile(user=user)重新绑定data。它不能同时是模型和会话数据。