Custom search

Issue #58 invalid
Former user created an issue

How can one search by a column that the model doesn't actually have?

Example: I have a model named 'Car', and it has three properties, brand, color and kms/miles. Now, when I create the view, I want to display 4 columns in Cars Table, so I use brand, color, kms/miles and a price that is calculated from the three other values.

Is there a way to search by the 'price' field, which is not in the model?

I'm going to override the prepare_results method and make a python search on the data.

If I'm not being clear here, please ask me anything.

Comments (1)

  1. Maciej Wisniowski repo owner

    This is more a question how to create a queryset with an extra field that will let you order by and filter by it. It is not directly related to django_datatables_view so I'm marking this as invalid.

    Anyway, what you can do is to use database functions provided by Django to create extra 'price' column with value calculated from the other columns you have. See https://docs.djangoproject.com/en/2.1/ref/models/database-functions/#module-django.db.models.functions

  2. Log in to comment