views.py - annotate for concatenating string values doesn't work

The following view doesn’t display the new column ‘Address’ nor deferring ‘addr1’, ‘addr2’, ‘addr3’.
request help for fixing the same. - Thank you!

Django version 4.0.6

class ListTableView(SingleTableView):
    table_class = ListTable
    queryset = List.objects.defer(
        'addr1',
        'addr2',
        'addr3',
        ).annotate(
        Address=Concat(
            'addr1',
            'addr2',
            'addr3',
            'City',
            'State', # Value(' - '),
            str('Pincode'), output_field=CharField(),
            ),
            )
    template_name = "list_table.html"

Couple different items:

  • That parent class (SingleTableView) isn’t one of the standard CBVs. Please post it here.

  • Also please post your template.

  • How do you know that those fields are not being deferred?

sure. Please find the details,

Template list_table.html:

{% extends "Dapp/base.html" %}

{% load render_table from django_tables2 %}
{% load i18n %}

{% block html_table %}active{% endblock %}

{% block table_main %}
<h1>List table</h1>
{% render_table table %}
{% endblock %}

Upon runserver the list shows all the columns of the model as the columns. Attaching image of the same.

I didn’t get what you meant by parent class (SingleTableView). Kindly explain. For, I am just following tutorials found in google and using it for our need. - Thank you!

Your view, ListTableView inherits from a class named SingleTableView.

There is no SingleTableView class in Django. That means that either you have it defined somewhere in your code, or it comes from a third-party package. Either way, I can’t diagnose problems that may exist in it without seeing it.

If it’s a class in your project, we need to see it here. If it comes from a third-party package, please let us know which one.

Likewise, you have the custom template tag, render_table. It’s also not a standard Django tag. We would need to see the source for it as well.

Right sir.

I am using django_tables2, django-filter, django-htmx & django-crispy-forms packages. Hope that class might be from one of these.

any possible help? or any alternative solutions? - Thank you!

That’s for you to determine. I’m guessing they’re from django_tables2, but you should be able to determine that.

It looks like the docs for it are at django-tables2 - An app for creating HTML tables — django-tables2 2.6.0 documentation. You should be able to find the answers to your questions in there.

(If I’m understanding your situation correctly, I think you’ll also want to read the section at Populating a table with data — django-tables2 2.6.0 documentation)

Wonderful! got it !
Thank you very much dear Ken!!

But I m stuck for concatenation of all those columns. After searching the google found a way for django-tables2 and modified my tables.py as below, but still not working for concatenation. Could you please help??
@KenWhitesell

class ListTable(tables.Table):
    addr1 = tables.Column(verbose_name="Address")
    class Meta:
        model = List
        fields = ("Donid", "Name", "addr1", "City", "State", "PAN", "Aadhar", "Mobile", "Email", "Remark")
        template_name = "django_tables2/bootstrap4.html"

        def render_addr1(self, record, value):
            return mark_safe(f"{value}, {record.City}")

as it is seen, the column City is not concatenated with the addr1 field. request your guidance. - Thank you!

I think the problem here may be that you have the render_addr1 method in the class Meta instead of at the class ListTable level. Try un-indenting that function.

Thank you very much Ken sir!! It worked :slight_smile: