Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the proper idiom for naming django model fields that are python reserved names?

Tags:

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?

like image 874
priestc Avatar asked Sep 18 '09 17:09

priestc


2 Answers

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)
>>> 
like image 60
jcdyer Avatar answered Nov 15 '22 07:11

jcdyer


Do you really want to use the db_column="complex" argument and call your field something else?

like image 23
joeforker Avatar answered Nov 15 '22 07:11

joeforker