I have a model that needs to have a field named complex
and another one named type
. Those are both python reserved names. According to PEP 8, I should name them complex_
and type_
respectively, but django won't allow me to have fields named with a trailing underscore. Whats the proper way to handle this?
views:
90answers:
2
+1
A:
Do you really want to use the db_column="complex"
argument and call your field something else?
joeforker
2009-09-18 18:18:43
+2
A:
There's no problem with those examples. Just use complex
and type
. You are only shadowing in a very limited scope (the class definition itself). After that, you'll be accessing them using dot notation (self.type
), so there's no ambiguity:
Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(object):
... type = 'abc'
...
>>> f = Foo()
>>> f.type
'abc'
>>> class Bar(object):
... complex = 123+4j
...
>>> bar = Bar()
>>> bar.complex
(123+4j)
>>>
jcdyer
2009-09-18 18:19:44
Technically, the examples you gave are not reserved words. They are builtins. Reserved words (like print or lambda) will cause actual problems.
jcdyer
2009-09-18 18:23:41