globals should show up in model methods and classes (like normal python classes)
|Reported by:||brenocon@…||Owned by:||adrian|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I want to do this:
import re class Note(meta.Model): #... def __repr__(self): if re.search(...)
Unfortunately, "re" isn't present in the namespace. The following exception occurs:
File "c:\brendano\djblog\apps\blog\models\blog.py", line 25, in __repr__ if re.search(r'^\s*$', self.title): NameError: global name 're' is not defined
as a workaround I can just do "import re" inside the method. But the larger problem is, all module-level (global) variables don't appear inside any method namespaces.
myglobal = 5 class Note(meta.Model): #... def __repr__(self): print myglobal
also gives the same error. In fact, a manual "global myglobal" statement fails to make it work.
IMPORTANCE: so I guess the preferred django way of doing things is to make the model classes as sparse and declarative as possible. In that case the bug is a minor inconvenience.
SUSPECTED CAUSE: maybe a bug in the ModelBase metaclass?
SVN version: 541
Change History (7)
comment:3 Changed 10 years ago by adrian
- Resolution set to invalid
- Status changed from new to closed
comment:4 Changed 10 years ago by brenocon@…
- Cc brenocon@… added
- Resolution invalid deleted
- Status changed from closed to reopened
- Summary changed from Module-level variables in model methods don't show up to globals should show up in model methods and classes (like normal python classes)
comment:5 Changed 10 years ago by adrian
- Resolution set to wontfix
- Status changed from reopened to closed