我在这里使用 django-datatable-view https://django-datatable-view.readthedocs.io/en/latest/index.html。
我遇到了一些问题,文档似乎没有说明原因。我的猜测是数据不是来自模型本身,而是添加到查询集中的注释字段。
我有一个包含许多字段的表,但有问题的表将是“活动”和“get_set”。
class RetailerStoresDatatable(Datatable):
geo = columns.TextColumn(
"Geo",
sources=["geo_set"],
processor="get_geo",
sortable=True
)
class Meta:
model = StoreAddress
page_length = 50
columns = [
...
"active",
]
processors = {
...,
"active": helpers.make_xeditable,
}
structure_template = "datatableview/bootstrap_structure.html"
def get_geo(self, instance, *args, **kwargs):
return render_to_string(
"administrator/retailer_stores_columns.html",
dict(column_key="get_geo", store=instance)
)
class AdminRetailserStoresView(XEditableDatatableView):
datatable_class = RetailerStoresDatatable
def get_queryset(self):
queryset = (
StoreAddress.objects.filter(...)
.annotate(
geo_set=Case(
When(geo_lat__isnull=False, geo_lng__isnull=False, then=True),
default=False,
output_field=BooleanField(),
),
)
)
return queryset
我按活动/不过滤,它适用于下面的 JS。真实的例子。
value = true
$('.datatable').DataTable().columns({{ columns_map|get_dict_item:"active" }}).search(JSON.parse(value)).draw()
我正在尝试与 geo 完全相同,但它只是忽略并返回所有已经存在的记录。设置 Geo 列是 True/False 文本。我将“geo”更改为“active”只是为了测试它过滤器是否正常。因此,如上所述,我认为这与它是一个带注释的字段而不是直接来自模型的事实有关。
$('.datatable').DataTable().columns({{ columns_map|get_dict_item:"geo" }}).search(value).draw()