morlandi / django-ajax-datatable

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

Multiple placeholder columns causes error. Single placeholder column works. #89

Open LegendaryFire opened 2 years ago

LegendaryFire commented 2 years ago

For some reason, when using placeholder columns, only one can be used. However, when two or more placeholder columns are required the following error is thrown.

Exception: Column 1 ("custom_column_2") is not orderable.

Here is some sample code.

    column_defs = [
        {
            'name': 'custom_column_1',
            'title': 'Custom 1',
            'placeholder': True,
            'orderable': False,
            'searchable': False,
            'visible': True,
        }, {
            'name': 'custom_column_2',
            'title': 'Custom 2',
            'placeholder': True,
            'orderable': False,
            'searchable': False,
            'visible': True,
        }, {
            'name': 'first_name',
            'searchable': True,
            'visible': False,
        }, {
            'name': 'last_name',
            'searchable': True,
            'visible': False,
        }, {
            'name': 'birth_month',
            'searchable': True,
            'visible': False,
        }, {
            'name': 'birth_day',
            'searchable': True,
            'visible': False,
        },

    ]
    def customize_row(self, row, obj):
        if obj.first_name and obj.last_name:
            row['custom_column_1'] = f'{obj.first_name} {obj.last_name}'
        else:
            row['custom_column_1'] = 'Unknown'

        if obj.birth_month and obj.birth_day:
            row['custom_column_2'] = f'{obj.birth_month}, {obj.birth_day}'
        else:
            row['custom_column_2'] = 'Unknown'

        return

Then, if I try setting custom_column_1 and custom_column_2 to be orderable, the following error occurs.

File "C:\Users\User1\PycharmProjects\djangoProject1\venv\lib\site-packages\django\db\models\sql\query.py", line 1677, in names_to_path raise FieldError( django.core.exceptions.FieldError: Cannot resolve keyword 'custom_column_2' into field. Choices are: first_name, last_name, birth_month, birth_day

morlandi commented 2 years ago

Thank you @LegendaryFire , I'll flag this as a bug to be investigated, but since I work on this project occasionally I can't guarantee any time schedule

LegendaryFire commented 2 years ago

Thanks for the prompt response @klavman. For any others who may experience the same issue, a temporary workaround would be to use a column which can be mapped to a field in the model instead of adding an additional placeholder column. See the example below.

    column_defs = [
        {
            'name': 'first_name',
            'title': 'Full Name',
            'searchable': True,
            'visible': True,
        }, {
            'name': 'last_name',
            'searchable': True,
            'visible': False,
        }, {
            'name': 'birth_month',
            'title': 'Birth Month & Day',
            'searchable': True,
            'visible': True,
        }, {
            'name': 'birth_day',
            'searchable': True,
            'visible': False,
        },

    ]
    def customize_row(self, row, obj):
        if obj.first_name and obj.last_name:
            row['first_name'] = f'{obj.first_name} {obj.last_name}'
        else:
            row['first_name'] = 'Unknown'

        if obj.birth_month and obj.birth_day:
            row['birth_month'] = f'{obj.birth_month}, {obj.birth_day}'
        else:
            row['birth_month'] = 'Unknown'

        return

Simply set the first_name and birth_month column to visible, and then override the values which are rendered to those cells. Of course, with the code in my first post, leave the last_name and birth_day columns as not visible so we can still use the values of them to search.

LegendaryFire commented 2 years ago

Update: Even with one placeholder, the problem still persists unfortunately.

morlandi commented 2 years ago

@LegendaryFire hopefully I'll be able to check this during the next weekend. The concept of "placeholder" comes from the original project I started with and to be honest I had rare occasions to use it

LegendaryFire commented 2 years ago

@morlandi No problem. I'll take a good look and see if perhaps I can come up with a solution as well. Thank you for your hard work, this really is the way to go when working with tables in Django

tboulogne commented 1 year ago

@morlandi hello hope you're well :-). Did you work on this placeholder bug ? I expérience the same problem for now. Thanks for all.

morlandi commented 1 year ago

Hello @tboulogne, nice to hear of you.

I haven't been actively involved in the project for some time because I'm busy with other activities. However, I keep an eye on the issues because sooner or later I hope to find the time to fix them, at least the most significant ones

I take this opportunity to wish you a happy new year ;)

Mario

tboulogne commented 1 year ago

Hello Mario,

I wish you a pleasant year too :-) ! Hope your best wishes comes to reality.

I hope to find some time to look into this too :-). Keep in touch.

Thierry

tboulogne commented 1 year ago

@morlandi No problem. I'll take a good look and see if perhaps I can come up with a solution as well. Thank you for your hard work, this really is the way to go when working with tables in Django

I think it's quite normal. We are on server-aide mode. Queryset did not know about placeholder field... as datatables knows... So placeholder should not be orderable or filterable...