django form queryset filter by current user -


i have made search form user can filter news accroding published date , crawlers have selected previously.. here form

class searchform(forms.form): pub_date_from = forms.charfield(label="from",max_length=20) pub_date_to = forms.charfield(label="to",max_length=30) crawler = forms.modelmultiplechoicefield(label="crawler",queryset=crawler.objects.filter(created_by=self.request.user) 

here want crawler shown user have selected previously..

here view..

class singlenewsview(listview): model = news form_class = searchform template_name = "single_news.html"  # def post(self, request, **kwargs): #   print "request" #   form = searchform(request.user)    def get(self, request, pk, **kwargs):      #form = searchform(request.user)     self.pk = pk      self.pub_from = request.get.get('pub_date_from',false)     self.pub_to = request.get.get('pub_date_to',false)     self.crawlers = request.get.get('crawler',false)     self.format = request.get.get('format',false)     print self.format      print self.crawlers       return super(singlenewsview,self).get(request,pk, **kwargs)    def get_context_data(self, **kwargs):      context = super(singlenewsview,self).get_context_data(**kwargs)     context["form"] = searchform     if self.pub_from , self.pub_to , self.crawlers:         qs = news.objects.filter(category_id=self.pk).filter(published_date__range=(self.pub_from,self.pub_to), crawler=self.crawlers)     else:         qs = news.objects.filter(category_id=self.pk)      context["something"] = qs     self.request.session['something_pks'] = [ i.details in qs ]      return context 

how can current user in form , filter according current user.. need help

just pass user form in view keyword argument.

just don't pass on in form init method. check out post:

passing **kwargs django form

you hand on request.user user_details passed form in example.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -