Michael Sanders
Michael Sanders

Reputation: 57

Django tables2: Skipping object from queryset possible?

A database table contains a column "body" of type "text". This column contains valid JSON data. Displaying data from this table using django-tables2 should skip objects depending on JSON elements in this text field.

This filter is obviously not possible using the usual Django QuerySet mechanisms. Therefore, I want to skip an object before rendering it into the table. Something like "before_render_row()" with the possibility to e.g. "return None" to skip the object.

Is there any possibility to do that? Thanks!

EDIT:

Table (DDL from SQlite, at Postgres "body" is type "jsonb"):

create table portal_d2c_messages
(
    slug              varchar(50) not null
        unique,
    record_created_dt datetime    not null,
    record_updated_dt datetime    not null,
    message_id        char(32)    not null
        primary key,
    device_mac        varchar(12) not null,
    sent_dt           datetime    not null,
    received_dt       datetime    not null,
    message_type      varchar(16) not null,
    c2d_queue_name    varchar(16) not null,
    body              text        not null,
    check ((JSON_VALID("body") OR "body" IS NULL))
);

View:

# Filter table based on JSON elements in "body".
qs = qs.filter(
    Q(body__icontains="TD1*") | Q(body__icontains="basket_id"),
    message_type__in=[
        dev_prot.D2CListMsg.MSG_TYPE_STR,
    ],
)

Upvotes: 0

Views: 31

Answers (0)

Related Questions