[Fixed]-Django custom form is invalid due to form init

1👍

I found the answer here: DJango form with custom __init__ not validating

I was missing:

super(PlayerAchievementForm, self).__init__(*args, **kwargs)
👤Atma

0👍

I had the same problem and after a lot of tries, the code that work for me is something like that:

(Notice the first instantiation of the the field at class level.)

class PlayerAchievementForm(forms.ModelForm):

    achievement = Achievement.objects.filter(input_type=0)

    class Meta:
        model = PlayerAchievement
        fields = ('achievement',)

    def __init__(self, *args, **kwargs):
        super(PlayerAchievementForm, self).__init__(**kwargs)
        self.fields['achievement'].queryset = Achievement.objects.filter(input_type=0)
👤koxta

Leave a comment