我有以下模型,该模型返回ManyToMany字段(approvers
)中的值作为Integerfield
(step
)中的选择:
from django.utils.functional import lazy
class Task(models.Model):
step = models.IntegerField(null=True)
approvers = models.ManyToManyField(
get_user_model(),
)
@property
def get_step_choices(self):
CHOICES = []
if self.approvers:
CHOICES = [(a.id, a.email) for a in self.approvers.all()]
return CHOICES
def __init__(self, *args, **kwargs):
super(Task, self).__init__(*args, **kwargs)
self._meta.get_field('step')._choices = lazy(
self.get_step_choices, list
)()
但是,我在 init 下使用的语法似乎无法将选择属性呈现给step
。我要去哪里错了?