命名作为 python 保留名称的 django 模型字段的正确习惯用法是什么?
我有一个模型,需要有一个名为 complex
的字段和另一个名为 type
的字段。这些都是 python 保留名称。根据 PEP 8,我应该分别将它们命名为 complex_
和 type_
,但 django 不允许我使用以下划线命名的字段。处理这个问题的正确方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些例子都没有问题。只需使用
complex
和type
即可。您仅在非常有限的范围内进行跟踪(类定义本身)。之后,您将使用点表示法 (self.type
) 访问它们,因此不会有歧义:There's no problem with those examples. Just use
complex
andtype
. 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:您真的想使用
db_column="complex"
参数并将您的字段命名为其他名称吗?Do you really want to use the
db_column="complex"
argument and call your field something else?