【问题标题】:Django enums visible within functions?Django枚举在函数中可见?
【发布时间】:2012-02-16 06:39:57
【问题描述】:

在我的模型中,我创建了一个 enun:

sc_goal_move_on = 0
sc_goal_cancel_project = 1
sc_goal_change_objectives = 2
sc_goal_other = 3

sc_review_goals = (
    (sc_goal_move_on, 'move_on'),
    (sc_goal_cancel_project, 'cancel_project'),
    (sc_goal_change_objectives, 'change_objectives'),
    (sc_goal_other, 'other')
)

当我将其定义为 choises=

时,它在类中可见
class project_phase(models.Model):
    phase = models.ForeignKey(phases)
    project = models.ForeignKey('project')
    date_start_plan_original = models.DateField(null=False, blank=False)
    date_end_plan_original = models.DateField(null=True, blank=False)
    is_closed = models.BooleanField()
    is_finished = models.BooleanField(default=False)
    is_reviewed_by_pmo = models.BooleanField(default=False)
    phase_review_goal = models.IntegerField(choices=sc_review_goals, null=True)

但我无法从模型的 def: 中访问它来生成一些 HTML 以使其在我的表单中可用。 cs_review_goals 或 models.cs_review_goals 都不起作用。

我想我很傻,在这里遗漏了一些简单的东西,请指教,谢谢!

【问题讨论】:

  • 这个问题已经回答了herehereherehere。另外,Python 没有 enums;你有一个tuple

标签: python django django-models enums


【解决方案1】:
class Student(models.Model):
    A_VAR = 'var content'

    def a_method(self):
        print self.A_VAR # prints 'var content'
        print Student.A_VAR # also prints 'var content'

【讨论】:

    【解决方案2】:

    您是否正在寻找从类方法中访问sc_review_goals 的权限?

    它看起来像是一个模块级别的变量,所以它应该可以从类方法中访问。

    如果您在类中定义它(如建议的那样),您可以通过self.sc_review_goalsproject_phase.sc_review_goals 访问它。

    您的问题可能是变量名是sc_review_goals,但您输入了cs_review_goals

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-22
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多