【发布时间】:2015-01-09 07:45:02
【问题描述】:
我想从 django 模型字段中调用一个 self 方法来获取选择选项。
我搜索了 stackoverflow 和 google 并找到了一些解决方案,但这些解决方案在我的情况下不起作用。这是一些已经存在的解决方案。 Django: Call self function inside a Django model.
这是我的模型:
MPL_CHOICE = (
("a", "assistance"),
("b", "Performed"),
("n", "Need assistance"),
("o", "Observed"),
)
class ProcedurName(models.Model):
my_choices = models.CharField(
"My Choices",
max_length=100,
null=False,
blank=False,
choices=self.mpl_choice_filed()
)
def mpl_choice_filed(self):
if self.pk:
avrage_acore = self.avrage_acore.all()
MPL_CHOICE_NEW = [(m.value,m.label) for m in minimun_avrage_acore]
return MPL_CHOICE_NEW
else:
return MPL_CHOICE
class ChoicesAfterEdited(models.Model):
label = models.CharField(
"MinimunAvrageScore Lavel",
max_length=250,
)
value = models.CharField(
"MinimunAvrageScore Value",
max_length=250,
)
procedure = models.ForeignKey(
ProcedureSetup,
null=True, blank=True,
related_name='avrage_acore'
)
如果您查看模型,在 my_choices 字段中,我尝试调用 self.mpl_choice_filed 方法,但出现此错误。
NameError: name 'self' is not defined
请帮我找出解决办法。
【问题讨论】:
标签: django django-models