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?
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)
>>>
Do you really want to use the db_column="complex"
argument and call your field something else?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With