django-ajax-datatable
django-ajax-datatable copied to clipboard
Column search does not work
Hello everyone. Added general search and automatically added column search. So, the general search works, but the column does not.
If you need more information, please tell me what
Thank you @maksam07 .. I confirm there are some problems in the search.
Just updated the demo project: http://django-ajax-datatable-demo.brainstorm.it/tracks/ and in that case the opposite happens: column search works, global search does not.
Must be a recent regression, since I've been using this for a while ;)
Let me check it; I'll be back to this issue with needed fix in a week or so
@maksam07 I double-checked this: both global and column searching seem working. Might be something related to your model. What I suggest is to add this to your settings:
AJAX_DATATABLE_TEST_FILTERS = True
then look at the runserver console, and see what happens while filtering.
In the demo site:
http://django-ajax-datatable-demo.brainstorm.it/tracks/
the error was related to the last columns "tags", which refers to a ManyToManyField.
Support for ManyToManyFields is still very limited, so for now I just disabled searching of that column.
The problem is specific to ManyToManyFields, and better addressed in #15
Hello @morlandi
when you disable searching for ManyToManyFields, is it affect on relational fields?
I am using this, and I have written something like this
class EarningsBalancesListDataTableView(AjaxDatatableView):
model = CashOutHistory
title = 'Cash Out History'
initial_order = [["user_id", "asc"], ]
length_menu = [[25, 50, 100, -1], [25, 50, 100, "All"]]
show_column_filters = False
disable_queryset_optimization = True
column_defs = [
{'name': 'user_id', 'title': 'User Id', 'visible': True, },
{'name': 'user', 'foreign_field': 'user__username', 'visible': True, },
{'name': 'earning_balances', 'title': "Earning Balances", 'visible': True, },
{'name': 'available', 'title': "Available", 'visible': True, },
{'name': 'pending', 'title': "Pending", 'visible': True, },
]
def get_initial_queryset(self, request=None):
earnings_details = self.model.objects\
.values('user__username', "user_id").annotate(
earning_balances=Sum(
Cast(F('amount'), output_field=FloatField())
)
)
return earnings_details
def customize_row(self, row, obj):
user_edit_url = reverse('admin_panel:model_edit_view', args=('Users', 'User', obj.get("user_id")))
row['user'] = f"<a href={user_edit_url} target='_blank'>{obj.get('user__username')}</a>"
row['earning_balances'] = format(obj.get('earning_balances'), '.2f')
row['user_id'] = obj.get("user_id")
row['pending'] = format(pending, '.2f')
row['available'] = "{:.2f}".format(available)
return
- I am showing some data from the CashOutHistory model and I have created these columns by executing the Django ORM query now I want to search that data from the search box at the front side but as I mentioned that these columns are custom made so it shows the error for the lookup fields.
- e.g django.core.exceptions.FieldError: Related Field got invalid lookup: icontains image: https://i.imgur.com/B72iZk5.png
Thank you @dmbackend ... I will reopen the issue to check this .. when time allows :wink:
Thanks for responding @morlandi, I am also finding some way to get this done.
I don't know whether its an issue or a lack of my information so I need a suggestion that if store data in html h1 tag e.g ("<h1 ->test<-/h1>") then it is rendering the data as HTML with bold and large text, is there any solution to turn on the auto escape in the ajax data table? Because it will break the security so need an quick solution.
did you set settings.AJAX_DATATABLE_STRIP_HTML_TAGS = False
?
yes, I checked with True and False values but it doesn't affect to output.
This is the relavant code (file columns.py, line 97):
def string_tags_in_case(self, value):
if STRIP_HTML_TAGS and value is not None:
return strip_tags(value)
return value
def render_column_value(self, obj, value):
if self._allow_choices_lookup:
#return self._choices_lookup[value]
return self.string_tags_in_case(self._choices_lookup.get(value, ''))
if isinstance(value, datetime.datetime):
value = format_datetime(value, True)
elif isinstance(value, datetime.date):
value = format_datetime(value, False)
elif isinstance(value, bool):
value = _('Yes') if value else _('No')
return self.string_tags_in_case(value)
can you spot a reason why you text shouldn't be treated as expected ?
strip_tags()
is supplied by Django:
from django.utils.html import strip_tags
sure, I will check and let you know the reason.