1

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

FortranMan
  • 31
  • 7
  • 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 Dec 12 '17 at 23:55
  • I already specified an expected result. It's the third sentence in my question. Do you really need my working django filter code? – FortranMan Dec 13 '17 at 00:13
  • 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 Dec 13 '17 at 00:16
  • This is Django-filter: https://django-filter.readthedocs.io/en/1.1.0/ This is an example of checkboxcolumn being used in Django-tables2: https://stackoverflow.com/questions/10850316/how-to-get-information-from-django-tables2-row Can I use the checkboxcolumn for a table returned via Django-filter? It's kind of a yes or no question. – FortranMan Dec 13 '17 at 00:18
  • 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 Dec 13 '17 at 00:23

2 Answers2

1

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>
FortranMan
  • 31
  • 7
  • In your examples you pass `filter.qs` to a `render_table`. This means you get a table generated from your django models. In order to add a custom column to a table, you must create a custom table object. This is explained in the [tutorial](http://django-tables2.readthedocs.io/en/latest/pages/tutorial.html). The table instance needs to go into the context and then passed to `{% render_table %}` – Jieter Dec 20 '17 at 13:18
  • Is my "custom table object" an entire table, or just a single column being added to my model? If it is a complete table, then I must pass my filtered queryset to this table object yes? I did go through the django-tables2 tutorials but this is a very basic example with no indicator of passing anything other than a simple model. – FortranMan Dec 21 '17 at 21:58
  • Checkbox column is now visible in my custom table, but trying to get django-filter data into table rather than full model; I will post full working code if I get it working. I am not sure whether I edit `tables.py` or `views` but I am trying. – FortranMan Dec 22 '17 at 00:25
  • Hi @Jieter , made some progress but edited my answer above – FortranMan Dec 29 '17 at 02:40
  • Hi @FortranMan, not sure what your exact question is. You code looks good. I'm not sure what your intention is with the copying and amending of the GET dict. Can you explain what you want to achieve there? – Jieter Dec 29 '17 at 08:46
  • @Jieter, The django-filter textbox which was before present and functional (without custom table) has disappeared. When I view source code it is not present either. I added a link to screen shot on my post above. – FortranMan Dec 29 '17 at 19:27
  • Ah, you need to add the filter to the context, currently table1 is the variable used in the template you posted. – Jieter Dec 29 '17 at 21:59
  • Yes, but things like "context" are still fuzzy for me. I think this is incorrect: '{{ Table1.form.as_p }}' but this is correct: '{% render_table table1 %}' – FortranMan Dec 30 '17 at 00:24
  • It works well now! Thank you so much! So, how do I mark this as "answered"? Also, do I "upvote" or something? – FortranMan Dec 30 '17 at 00:31
  • Context is the third parameter you pass to render(). It's a dict of variables which will be available in the template. – Jieter Dec 30 '17 at 10:46
0

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.

Jieter
  • 4,101
  • 1
  • 19
  • 31
  • Thank you, I posted a second comment but wasn't sure where to put it. This is my first post here so I'm trying to figure it all out. – FortranMan Dec 19 '17 at 23:02