morlandi / django-ajax-datatable

A Django app which provides the integration of a Django project with the jQuery Javascript library DataTables.net
MIT License
208 stars 64 forks source link

Column search does not work #14

Open maksam07 opened 3 years ago

maksam07 commented 3 years ago

Hello everyone. Added general search and automatically added column search. So, the general search works, but the column does not.

search1 search2

If you need more information, please tell me what

morlandi commented 3 years ago

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

morlandi commented 3 years ago

@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.

morlandi commented 3 years ago

The problem is specific to ManyToManyFields, and better addressed in #15

varduhi-sargsyan commented 2 years ago

Hello @morlandi

when you disable searching for ManyToManyFields, is it affect on relational fields?

dmbackend commented 2 years ago

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
morlandi commented 2 years ago

Thank you @dmbackend ... I will reopen the issue to check this .. when time allows :wink:

dmbackend commented 2 years ago

Thanks for responding @morlandi, I am also finding some way to get this done.

dmbackend commented 2 years ago

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.

morlandi commented 2 years ago

did you set settings.AJAX_DATATABLE_STRIP_HTML_TAGS = False ?

dmbackend commented 2 years ago

yes, I checked with True and False values but it doesn't affect to output.

morlandi commented 2 years ago

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
dmbackend commented 2 years ago

sure, I will check and let you know the reason.