[Fixed]-Access request from the forms.Form to get data from dB related to user

1👍

You can pass request to your form by changing your __init__ method like this :

class InformForm(forms.Form):
...
   def __init__(self, user=None,*args, **kwargs):
        super(InformForm, self).__init__(**kwargs)
        if user:  
            self.fields['somefield'] = forms.ChoiceField()                  
            self.fields['somefield'].widget = forms.Select()
            self.fields['somefield'].queryset = Someobject.objects.filter(User=user)
...

If the User is linked to other object in db by Foreign key, then you will get all the values of other object as select items.

Also , when creating form you could pass user like this :

form= InformForm(user=request.user,data=request.POST)

Leave a comment