我的表单中有一个“联盟类型”字段,我想将其设为readonly
。我使用了widget.attr['readonly'] = True
,但它不起作用。在
模型:
class League(models.Model):
league_types = (
('league', 'League'),
('knockout', 'Knockout'),
)
....
season = models.ForeignKey(Season, related_name = "league_season")
league_type = models.CharField(max_length=10, choices = league_types, default ='league')
...
ModelForm
:
在视图.py公司名称:
if League.objects.filter(season = season, league_type = 'knockout').count():
form = LeagueForm(initial={'season': season, 'league_type': 'league'})
form.fields['league_type'].widget.attrs['readonly'] = True
else:
form = LeagueForm(initial={'season': season})
更新:
我不能使用disabled属性,因为我要创建一个具有league_type初始值的表单。在
目前没有回答
相关问题 更多 >
编程相关推荐