1 回答
TA贡献1796条经验 获得超10个赞
您可以覆盖 您的 来处理逻辑。像这样:__init__AnimalForm
class AnimalForm(forms.ModelForm):
field_choices = [
(1, 'CACHORRO'),
(2, 'GATO'),
(3, 'OUTRO'),
]
field_choices_subset = [
(1, 'CACHORRO'),
(3, 'OUTRO'),
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if kwargs.get("instance") and kwargs.get("instance").pessoa.nome.startswith("a"):
self.fields["tipo"].choices = field_choices_subset
name = forms.CharField(max_length=100)
tipo = forms.ChoiceField(choices=field_choices)
custo = forms.DecimalField(max_digits=7, decimal_places=2)
class Meta:
prefix = 'animal'
model = Animal
fields = ('name', 'tipo', 'custo')
添加回答
举报
