1

I have written a form like this where I only want to show the crawlers selected by current users. My form looks like

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.all())

Here I have listed all the crawlers but I only want the crawlers selected by current user. I am using this as a search form where he can only search from the crawler he have selected previously. How can I do that. I have used this form as context to search.

My view looks like this..

class SingleNewsView(ListView):
model = News
form_class = SearchForm
template_name = "single_news.html"



def get(self, request, pk, **kwargs):
    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#(self.request.GET)
    if self.pub_from and self.pub_to and self.crawlers:
        context["something"] = News.objects.filter(category_id=self.pk).filter(published_date__range=(self.pub_from,self.pub_to), crawler=self.crawlers)
    else:
        context["something"] = News.objects.filter(category_id=self.pk)

    return context

Can anyone tell me how can I get only the crawler selected by current users.

Aaeronn
  • 155
  • 1
  • 3
  • 12

1 Answers1

1

When you instantiate the form you will have to pass in the objects you want to use for the choices, which you can get in the view with: self.request.user.crawler_set.all(). Then I guess you can pass in the objects and set them as the field choices in the forms __init__.

So your search form would look similar to this..

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.all())

    def __init__(self, user, *args, **kwargs):
        if user:
            self.fields['crawler'].queryset = user.crawler_set.all()

and you'd instantiate the form in your view with:

    context["form"] = SearchForm(self.request.user, instance=self.request.GET)
ptr
  • 3,292
  • 2
  • 24
  • 48
  • I have shared you the view can you please tell where can I get self.request.user.crawler_set.all().. I m not getting where to use it.. – Aaeronn Jun 25 '14 at 09:35
  • anywhere, the `self` in `self.request.user.crawler_set.all()` refers to the instance of your class-based view, so you can call it in `get_context_data` just before you instantiate the form or any where else you can access the instance members – ptr Jun 25 '14 at 09:47
  • I used it in context["form"] = SearchForm(self.request.user).. Then it says 'User' object has no attribute 'get'... – Aaeronn Jun 25 '14 at 09:52
  • because your code `SearchForm#(self.request.GET)` shows your `SearchForm` is expecting request.GET as its first argument, you would have to pass in the user as an additional argument and update the form's `__init__` method to accept it. – ptr Jun 25 '14 at 09:56
  • what if I do this and dont work context["form"] = SearchForm(self.request.user). This way I m passing the user.. – Aaeronn Jun 25 '14 at 09:58