python - Django: Access request.GET in form to pass queryset as choices -


how in django can access requset in form? need data tuple pass in choices form. below init approach doesn't work: nameerror: name 'request' not defined, self or without: self.request.get.get('project') or request.get.get('project')

class postfilterform(forms.form):     def __init__(self, *args, **kwargs):         self.request = kwargs.pop("request")         super(postfilterform, self).__init__(*args, **kwargs)      monitoring_words_to_show = nlpmonitorword.objects.filter(monitoringwords__name = self.request.get.get('project'))      words_list = []     word in monitoring_words_to_show:         words_list.append((word.monitor_word, word.monitor_word))         words_list = tuple(words_list)   # trying here tuple pass in choises (('vk', 'vk'), ('fb', 'fb'), ('vkfb', 'vkfb'))      project = forms.charfield(required=true, label='')     monitor = forms.multiplechoicefield(widget=forms.selectmultiple, choices=words_list, required=false, label='') 

all code you're trying use isn't used within method means doesn't belong instance of postfilterform , therefore has no knowledge of self let alone fields.

you should include these in function, although function should unclear.

def my_function(self):     monitoring_words_to_show = nlpmonitorword.objects.filter(monitoringwords__name = self.request.get.get('project'))      words_list = []     word in monitoring_words_to_show:         words_list.append((word.monitor_word, word.monitor_word))         words_list = tuple(words_list)   # trying here tuple pass in choises (('vk', 'vk'), ('fb', 'fb'), ('vkfb', 'vkfb')) 

Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -