Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ajax_datatable/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def string_tags_in_case(self, value):
def render_column_value(self, obj, value):

if self._allow_choices_lookup:
#return self._choices_lookup[value]
# return self._choices_lookup[value]
return self.string_tags_in_case(self._choices_lookup.get(value, ''))

if isinstance(value, datetime.datetime):
Expand Down Expand Up @@ -226,7 +226,7 @@ def render_column(self, obj):
class ManyToManyColumn(ForeignColumn):

def get_foreign_value(self, obj):
current_value = obj
current_value = obj # noqa
m2m_name, m2m_field = self._field_path

to_eval = f'obj.{m2m_name}_list'
Expand All @@ -243,6 +243,7 @@ def render_column_value(self, obj, value_list):

return ', '.join([str(value) for value in value_list])


class ColumnLink(object):

def __init__(self, name, model_column=None, searchable='true', orderable='true', search_value='',
Expand Down
15 changes: 15 additions & 0 deletions ajax_datatable/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ def build_column_filter(column_name, column_obj, column_spec, search_value, glob
# # search_filter = Q(**{query_param_name + '__in': [search_value, ]})
# raise Exception('Searching not supported for ManyToManyFields (yet)')
# pass
elif isinstance(column_obj.model_field, models.BooleanField):
query_param_name = column_obj.get_field_search_path()
if search_value in ['true', 'false']:
search_value = eval(search_value.capitalize())
search_filter = Q(**{query_param_name: search_value})
else:
# If the filter contains 'y', 'e', 's' or a combination we filter for True
if search_value in 'yes':
search_filter = Q(**{query_param_name: True})
# same but with 'n', 'o'
elif search_value in 'no':
search_filter = Q(**{query_param_name: False})
# if we have any other search value, we exclude both True and False, should return empty
elif search_value:
search_filter = ~Q(**{f'{query_param_name}__in': [True, False]})
else:
query_param_name = column_obj.get_field_search_path()

Expand Down
3 changes: 3 additions & 0 deletions ajax_datatable/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ def initialize(self, request):
cs['choices'] = choices if len(choices) > 0 else None
# (3) Otherwise, just use the sequence of choices that has been supplied.

if choices and not [col for col in column_defs_ex if col['name'] == key and 'lookup_field' in col]:
cs['lookup_field'] = '__iexact'

self.column_index[key] = {
'spec': cs,
'column': column,
Expand Down
1 change: 1 addition & 0 deletions example/backend/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class TagAdmin(admin.ModelAdmin):
pass


@admin.register(Tag2)
class Tag2Admin(admin.ModelAdmin):
pass
Expand Down
1 change: 1 addition & 0 deletions example/backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __str__(self):
('new-age', 'New Age'),
)


class Tag2(models.Model):
name = models.CharField(null=False, blank=False, max_length=256, choices=TAG2_CHOICES)

Expand Down
8 changes: 7 additions & 1 deletion example/frontend/ajax_datatable_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ class TrackAjaxDatatableView(AjaxDatatableView):
'visible': True, 'choices': True, 'autofilter': True, },
# {'name': 'tags', 'visible': True, 'searchable': False, },
{'name': 'tags', 'm2m_foreign_field': 'tags__name', 'searchable': True, 'choices': True, 'autofilter': True, },
{'name': 'tags2', 'm2m_foreign_field': 'tags2__name', 'searchable': True, 'choices': True, 'autofilter': False, },
{
'name': 'tags2',
'm2m_foreign_field': 'tags2__name',
'searchable': True,
'choices': True,
'autofilter': False,
},
]

@query_debugger
Expand Down