Django在函数中可见吗?

时间:2012-01-22 16:49:47

标签: python django django-models enums

在我的模型中,我创建了一个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都不起作用。

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

2 个答案:

答案 0 :(得分:0)

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

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

如果您在课程中定义了它(按照建议),则可以通过self.sc_review_goalsproject_phase.sc_review_goals访问它。

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

答案 1 :(得分:0)

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'