[Answered ]-How do I get an item into a hidden form field in django?

1👍

This should help you.

class NewIssueForm(forms.ModelForm):

    def __init__(self,*args, pk,school, **kwargs):
        super(NewIssueForm, self).__init__(*args, **kwargs)

        self.fields['borrower_id'].queryset = Student.objects.filter(
            school=school)

        self.fields['book_id'].initial = pk #Sets the field with the pk and it's hidden again

    class Meta:
        model = Issue
        fields = ['book_id','borrower_id']
        widgets = {
            'book_id':forms.TextInput(attrs={"'type':'hidden'}),

        }
👤Ptar

Leave a comment