Django:' readonly'属性不适用于我的ModelForm

时间:2017-10-26 14:44:58

标签: python django django-forms modelform

有一个' league_type'我的表单中的字段,我想让它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

class LeagueForm(forms.ModelForm):
   class Meta:
      model = League
      fields = ('title', 'league_type', 'season', 'status')
      widgets = {'season':forms.HiddenInput(),}

view.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})

更新

我无法使用已禁用属性,因为我要创建一个具有league_type初始值的表单。

0 个答案:

没有答案