我正在学习django,这是我创建的模型class UserProfile(models.Model):
name = models.CharField(max_length=50, verbose_name="Name")
login = models.CharField(max_length=25, verbose_name="Login")
password = models.CharField(max_length=100, verbose_name="Password")
phone = models.CharField(max_length=20, verbose_name="Phone number")
def __str__ (self):
return self.name
因为name是一个静态变量,所以在方法__str__中,也可以这样调用它
^{pr2}$
但是当我尝试使用上面的方法访问变量时,我得到了以下错误AttributeError at /admin/TasksManager/userprofile/
type object 'UserProfile' has no attribute 'name'
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/TasksManager/userprofile/
Django Version: 1.6
Exception Type: AttributeError
Exception Value:
type object 'UserProfile' has no attribute 'name'
Exception Location: C:\Users\X\django_book\Work_manager\TasksManager\models.py in __str__, line 18
Python Executable: C:\Users\X\django_book\Scripts\python.EXE
Python Version: 3.4.3
Python Path:
['C:\\Users\\X\\django_book\\Work_manager',
'C:\\Windows\\system32\\python34.zip',
'C:\\Users\\X\\django_book\\DLLs',
'C:\\Users\\X\\django_book\\lib',
'C:\\Users\\X\\django_book\\Scripts',
'C:\\Python34\\Lib',
'C:\\Python34\\DLLs',
'C:\\Users\\X\\django_book',
'C:\\Users\\X\\django_book\\lib\\site-packages']
Server time: Thu, 7 Jan 2016 23:10:34 +0530
为什么呢??在
我在下面的课上应用了同样的技巧,效果非常好class Rect:
status = "this is test"
def __init__(self, l, b):
self.l = l
self.b = b
def something(self):
return Rect.status
为什么会有矛盾?在