Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django forms: rendering different data in forms per users login

Hello i’m currently making a webiste in Django, is it possible to render different select fields by querying the objects atributed in user via request.user.id to obtain and get the datas inputted by the user instead of all data gathered on all users? thank you so much

class clientforms(forms.Form):
  projectfield = forms.ModelChoiceField(queryset= Project.objects.all(),
    widget=forms.Select(attrs={
        'class' : 'form-control',
    })
    )

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

credits to original answer :
Django forms: rendering different data in forms per users login

based on the previous answer on the link you must override the original forms

forms.py

class clientforms(forms.Form):
 projectfield = forms.ModelChoiceField(queryset= Project.objects.all().filter(profile=1),
    widget=forms.Select(attrs={
        'class' : 'form-control',
    })
    )
    #add the overriding function
    def __init__(self, user, *args, **kwargs):
        super(clientforms, self).__init__(*args, **kwargs)
        self.fields['projectfield'].queryset=Project.objects.all().filter(profile=Profile.objects.get(user_id=user.id))

on your views.py

# add the request.user on your forms
user = request.form
form = clientforms(user)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading