TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How to use Enums for Django Field.choices

1 点作者 rodwyer超过 11 年前

1 comment

boohoofoodoo超过 11 年前
Not really the same but I typically just do something like this to prevent magic numbers sprinkled all over my consuming code...<p>&lt;code&gt;<p>class Section(TransformerCommon): &quot;&quot;&quot; working model &quot;&quot;&quot; TEMPERATURE_COLD = 20<p><pre><code> NONE = 0 END = 1 BOTH = 2 END_INSULATION_LOC_CHOICES = ( (NONE, &quot;None&quot;), (END, &quot;End&quot;), (BOTH, &quot;Both&quot;), )</code></pre> ... end_ins_location = models.IntegerField( choices=END_INSULATION_LOC_CHOICES, verbose_name=&quot;End Insulation Location&quot;) ... try: if self.end_ins_location == Section.NONE: ... elif self.end_ins_location == Section.END: ... elif self.end_ins_location == Section.BOTH: ... else: ...<p>&lt;&#x2F;code&gt;