django - Unexpected behavior of static variables in python -
this question has answer here:
- django models & python class attributes 4 answers
i learning django, here model have created
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
since name
static variable, in method __str__
, can called this
def __str__ (self): return userprofile.name
but when tried access variable using above method , following error
attributeerror @ /admin/tasksmanager/userprofile/ type object 'userprofile' has no attribute 'name' request method: 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
why ??
i applied same technique in following class, works fine
class rect: status = "this test" def __init__(self, l, b): self.l = l self.b = b def something(self): return rect.status
why there contradiction ?
the django model not contain static variables. used metaclass define actual instance variables. can't access them through class.
Comments
Post a Comment