1
votes

I want to have a checkboxcolumn in my returned table via Django-filter, then select certain rows via checkbox, and then do something with these rows.

This is Django-filter: django-filter.readthedocs.io/en/1.1.0 This is an example of checkboxcolumn being used in Django-tables2: stackoverflow.com/questions/10850316/…

My question is: can I use the checkboxcolumn for a table returned via Django-filter?

Thanks

2
please insert some properly formatted code, use an example, link it, specify an expected result, an error, anything really, other than text. - 8-Bit Borges
I already specified an expected result. It's the third sentence in my question. Do you really need my working django filter code? - FortranMan
all I'm saying is that SO expects questions with code. long questions with plain text look fastidious. perhaps you could research elsewhere and move it to SO when an actual snippet is involved. - 8-Bit Borges
This is Django-filter: django-filter.readthedocs.io/en/1.1.0 This is an example of checkboxcolumn being used in Django-tables2: stackoverflow.com/questions/10850316/… Can I use the checkboxcolumn for a table returned via Django-filter? It's kind of a yes or no question. - FortranMan
I can't help you with this matter, I'm afraid. I was just reviewing your question. I suggest you edit your question and insert the links provided to improve your chance of being answered. good luck. - 8-Bit Borges

2 Answers

1
votes

Full working code:

filters.py:

from project_django.models import SomeModel
import django_filters

class UserFilter(django_filters.FilterSet):

    class Meta:
        model = SomeModel
        fields = ['jobnumber', ]

views.py:

def custom_table(request):

    user_list = SomeModel.objects.all()

    data = request.GET.copy()

    if len(data) == 0:
        data['jobnumber'] = 0

    user_filter = UserFilter(data, queryset=user_list)

    table1 = JobTable(user_filter.qs)

    # WORKING: custom table with checkbox
    RequestConfig(request).configure(table1)

    # WORKING: custom table with checkbox
    return render(request, 'index.html', {'table1': table1, 'filter': user_filter})

tables.py:

import django_tables2 as tables
from .models import SomeModel


class JobTable(tables.Table):

    selection = tables.CheckBoxColumn(accessor='pk')
    #jobnumber = tables.Column()
    class Meta:
        model = SomeModel

index.html:

{% load widget_tweaks %}
{% block content %}

  <form method="get">
    <div class="well">
      <h4 style="margin-top: 0">Filter</h4>
        <div class="row">
        <div class="form-group col-sm-4 col-md-3">
        {{ filter.form.as_p }}
        <button type="submit">Search</button>
        </div>
        </div>
      </div>
    </div>
  </form>

  <form action="roll.html" method="post">
      {% render_table table1 %}
      <input type="submit">

  </form>
0
votes

What django-filter does from the perspective of django-tables2 is supplying a different (filtered) queryset. django-tables2 does not care about who composed the queryset, it will just iterate over it and render rows using the models form the queryset.

So if you a checkbox column to the table or not, or use django-filter or not, django-tables2 will just render any queryset it gets.

If you want to use the checked records for some custom filter, you'll have to do some manual coding, it's not supported out of the box.

Short answer: yes, you can use django-tables2 with a CheckboxColumn together with django-filter.