首先请看下面的代码:
Project = models.ForeignKey(Project,null=False, blank=True)
if Porject is 'A':
Owner = models.CharField(max_length=100, choices=**owner_set_A**)
else:
Owner = models.CharField(max_length=100, choices=**owner_set_B**)
所以所有者选择应该从owner_set_A切换到B,它依赖于Project的值。 我告诉我我该怎么做 感谢Timmy的回复,但我应该怎么做models.Model
class Task(models.Model):
project = models.ForeignKey(Project,null=False, blank=True)
if Porject is 'A':
Owner = models.CharField(max_length=100, choices=**owner_set_A**)
else:
Owner = models.CharField(max_length=100, choices=**owner_set_B**)
有没有办法获得项目字段值?
答案 0 :(得分:1)
您不需要两个单独的字段。该字段只保存数据,而您需要过滤用户在其表单中显示的choices
内容。例如,如果您使用的是django管理员,则可以执行类似(未经测试的)
class MyModelForm(forms.ModelForm):
def __init__(self, *args, *kwargs):
super(MyModel, self).__init__(args, kwargs)
if self.fields['project'].foo == "bar":
self.fields['owner'].choices = ((0, "X"), (1, "Y"),...)
else:
self.fields['owner'].choices = ((0, "A"), (1, "B"),...)
class Meta:
model = MyModel
<强> admin.py 强>
class MyModelAdmin(admin.ModelAdmin):
form = MyModelForm