Django:当我尝试删除用户时,AttributeError [英] Django: AttributeError when I try to delete a user

查看:182
本文介绍了Django:当我尝试删除用户时,AttributeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从管理页面(Django 1.5)中删除用户时,我有一个有趣的错误:



AttributeError at / admin / teaching / student / 5 / delete /



'tuple'对象没有属性'replace' code>



其次是一个很长的追溯我不明白,最后一个关于

  line 43 in ... / site-packages / django / utils / html.py in escape:
return mark_safe(force_text(text).replace('&', '('','&'')替换('&','&') ).replace(','''))

text 只是字符串:格式化错误:强制转换为Unicode:需要字符串或缓冲区,元组发现所以 force_text 正在返回一个元组,与我的模型有什么关系?我很困惑。



我的用户是学生,每个学生模型都是哈是一个具有User模型的OneToOneField,所以我猜对应的Student对象也必须被删除。我可以从shell中删除用户没有任何问题(并且学生对象也消失)。



编辑:这里是学生 model:

  class Student(models.Model):
user = models.OneToOneField用户)
start_year = models.IntegerField()
name = models.CharField(max_length = 100)
token = models.CharField(max_length = 20,blank = True,null = True)

def __unicode __(self):
return self.name,

def user_email(self):
return self.user.email


解决方案

发现!如果它不是打字错误,你的 __ unicode __ return子句中的逗号会使它返回一个元组,这样就是错误你会得到。

  def __unicode __(self):
#注意tralining逗号
#return self.name,
#should be like this(no comma)
return self.name

希望这样工作!


I've got a funny error when I try to delete a user from the admin pages (Django 1.5):

AttributeError at /admin/teaching/student/5/delete/

'tuple' object has no attribute 'replace'

followed by a long Traceback I don't understand, ending with a complaint about

line 43 in .../site-packages/django/utils/html.py in escape:
return mark_safe(force_text(text).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;'))

but text is just the string: Error in formatting: coercing to Unicode: need string or buffer, tuple found. So force_text is returning a tuple? What's that got to do with my models? I'm confused.

My users are students, and each Student model is has a OneToOneField with the User model, so I guess the corresponding Student object has to be deleted as well. I can delete the User from the shell with no problem at all (and the Student object disappears as well).

Edit: here's the Student model:

class Student(models.Model):
    user = models.OneToOneField(User)
    start_year = models.IntegerField()
    name = models.CharField(max_length=100)
    token = models.CharField(max_length=20, blank=True, null=True)

    def __unicode__(self):
        return self.name,

    def user_email(self):
        return self.user.email

解决方案

Spotted! If it's not a typo, the trailing comma in your __unicode__ return clause is making it return a tuple so that's the error you're getting.

def __unicode__(self):
    # note the tralining comma
    #return self.name,
    #should be like this (no comma)
    return self.name

Hope this works!

这篇关于Django:当我尝试删除用户时,AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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