Django ChoiceField initial setup not working -
i have modelform use django forms.choicefield. writing value database works. when open url, dropdown list not showing selected value selected value.
i tried setting initial=value, it's not working well.
class gameform(forms.modelform): gamecode = forms.choicefield(required=false) def __init__(self, *args, **kwargs): obj = allgamecodes.objects.filter(game=game) choices = [] choices.append(('', '-----------')) in obj: choices.append((i.code,i.description)) self.fields['gamecode'].choices = choices in views.py, game = games.objects.get(id=1) form = gameform(request.post, initial={'code':game.code}
you must take game
variable kwargs
. using modelchoicefield
may ease solution
def __init__(self, *args, **kwargs): super(gameform, self).__init__(*args, **kwargs) _game = kwargs.get("game"): if _game: self.fields['gamecode'] = modelchoicefield(queryset=allgamecodes.objects.filter(game=_game), required=false)
Comments
Post a Comment