asifpy / django-crudbuilder

Generic CRUD implementation in Django
https://django-crudbuilder.readthedocs.org/en/latest/index.html
Apache License 2.0
193 stars 67 forks source link

custom table filter #34

Closed oziek88 closed 7 years ago

oziek88 commented 7 years ago

Hey guys,

I'm trying to add a browsing section that mimics the way the crudbuilder searchbar works. I have multiple inputs and I'd like to tie each input to a separate object in my database. For example, with the "days_since_last_login" search bar, you could type 33 and it will only show the results with days since last login equal to 33. I have it to where the search bar can put the results onto the page, but I can't get it to stay a part of the crudbuilder table. I'd like it to work exactly the way the default searchbar works in the main_content block. But I'm unsure how to render to the main_content block. Any help or advice would be amazing. Let me know if you'd like to see any pieces of my code and I can pin them up for you. Thank you!

Update: I'm trying to alter the search input form by customizing my list view. I'm trying to do so with the use of the custom_queryset classmethod. I'd like to add in additional request.GET commands for other input textboxes. When doing so, I get an error that my Crud class has no attribute 'request', but I've defined it as @classmethod def custom_queryset(cls, request, **kwargs): name = cls.request.GET.get('name')

Essentially, if I could go into crudbuilder/mixins.py, I'd want to change get_queryset to something along the lines of `def get_queryset(self):

    if self.custom_queryset:
        objects = self.custom_queryset(self.request, **self.kwargs)
    else:
        objects = self.model.objects.all()
        name = self.request.GET.get('name')
        gender = self.request.GET.get('gender')
    if name:
        q_list = [
            Q(
                ('{}__icontains'.format(field), name))
            for field in self.crud.search_fields
        ]
        objects = objects.filter(reduce(operator.or_, q_list))
    if gender:
        q_list = [
            Q(
                ('{}__icontains'.format(field), gender))
            for field in self.crud.search_fields
        ]
        objects = objects.filter(reduce(operator.or_, q_list))
    return objects.order_by('-id')`
oziek88 commented 7 years ago

problem solved. I was able to put it all inside def get_queryset within a CustomListView class.