0

so basically i have this generic view that inherits from ListView and what i want it do to is to take some sort of argument (like a string or "options") and then filter my model depending on those arguments.

I've looked for two days and can't seem to find much about this. I've played around with overwriting the get_queryset function also just tried filtering in directly like so:

  model =  product.objects.filter(pk__in=[1,2,3,4,5])

however most of the times it just gives me this error:

/python3.5/site-packages/django/views/generic/list.py", line 38, in get_queryset

    queryset = self.model._default_manager.all()
AttributeError: 'QuerySet' object has no attribute '_default_manager'

I don't really need an "solution" i'd be just fine if someone just could point me to where i can read in deapth about this since i've only managed to find basic description.

Thanks

cezar
  • 11,616
  • 6
  • 48
  • 84
Bolian
  • 41
  • 8
  • 1
    can you add more details about your models ? – Chiheb Nexus Jul 19 '17 at 02:01
  • 3
    what is product ?. Is that a name of model or variable name. It seems that product is variable name(assigned to a queryset) and in that case it should be product.filter(pk__in=[1, 2, 3, 4, 5]) – Raj Subit Jul 19 '17 at 02:34
  • product is the name of the model, "from .models import product" – Bolian Jul 19 '17 at 03:54
  • 1
    Have you tried `model = product` then override the `get_queryset()` method?? The above error is showing because you have defined the `model` variable as a `queryset`. – zaidfazil Jul 19 '17 at 04:37
  • thanks! lol that was much easier than i thought, why i hadnt tried just putting the filter as return was because i thought if i overwrote the function i had to write the whole function + that so i destined to find the original defenition of the function but couldnt. – Bolian Jul 19 '17 at 07:06

3 Answers3

1

Just split it into 2 lines:

model = Product

and

queryset = Product.objects.filter(pk__in=[1,2,3,4,5])

It will work.

Piyush Maurya
  • 1,945
  • 16
  • 26
  • Check your model class, is it `product` or `Product` and go with that name. As per convention, we use `CamelCase` for class. – Piyush Maurya Jul 19 '17 at 06:25
1

Try,

class YourView(ListView):
    model = product

    def get_queryset(self):
        queryset = super(YourView, self).get_queryset()
        #your condition here.
        return queryset.filter(pk__in=[1,2,3])
zaidfazil
  • 9,017
  • 2
  • 24
  • 47
  • I've done it like this right now: def get_queryset(self, **kwargs): return product.objects.filter(**kwargs) model = product and it seems to work, is it really important to super(just curius)? Also how do i send kwargs to a view? like an argument for a search string or is it better to just use post method? Thanks for your help! (Edit: The ident got messed up in this comment, oh well...) – Bolian Jul 19 '17 at 07:10
  • `super()` call just loads the default queryset. It is just conventional to just call the parent class method, you could just ignore it, but it is actually a good practice. Then, about passing kwargs, you could write your urls to grab a specific pattern and use that as kwarg, that'd be ugly though. But, personally I'd suggest writing a custom manager for filtering the queryset (only if filtering according to some passed kwarg is needed). – zaidfazil Jul 19 '17 at 07:28
  • Well basically i'm trying making a store and the user has the ability to either type in a search string or check different categories. If this was php i guess i'd post everything then just check for values that arent none then build a query. I decided to dabble around with kwargs because i thought it be easier +i saw in docs this "filter(**kwargs). I kinda wanna make it like a unix command where "ls" is the view and "-" (dashes) are options or in this case filtering. However i'm not sure if this is how they've implemented it... I might just need to do a post or check the custom manager thingy. – Bolian Jul 19 '17 at 07:42
0

implement an extra function in your "views.py" to handle the search

qiang
  • 1
  • Hi, welcome to stack-overflow . you can use this link for improve your answer https://stackoverflow.com/help/how-to-answer and https://stackoverflow.com/tour – Amirhossein Apr 24 '20 at 04:06