global search function not filtering as expected

Issue #27 closed
Marc created an issue

Hi, I'm having a problem with search feature.

assuming below:

TABLE A
| ID | DUTY |
|  1 |  Q1  |
|  2 |  Q2  |

TABLE B
| ID | LOG | A_ID |
|  1 |  A  |   1  |
|  2 |  B  |   2  |
|  3 |  C  |   2  |
|  4 |  D  |   1  |

TABLE C
| ID | ACTION | A_ID |
|  1 |    A   |   1  |
|  2 |    B   |   2  |
|  3 |    C   |   2  |

I created a flat table and only taking the first element of the related table. I accomplish it with below code.

class DutyTableJson(DatatableView):
    def get_initial_queryset(self):

        return self.model.objects.annotate(log=F('table_b__log'),
                                           action=F('table_c__action')).\
            all()

DISPLAY TABLE

| ID | DUTY | LOG | ACTION |
|  1 |  Q1  |  D  |    A   |
|  2 |  Q2  |  C  |    C   |

Assuming I'm using the global search. search string: D

EXPECTED OUTPUT

| ID | DUTY | LOG | ACTION |
|  1 |  Q1  |  D  |    A   |

ACTUAL OUTPUT

| ID | DUTY | LOG | ACTION |
|  1 |  Q1  |  D  |    A   |
|  1 |  Q1  |  D  |    A   |

For individual column search things are working as expected.

Thank you.

Comments (5)

  1. Marc reporter

    I found what's the problem, it's with my initial queryset.

            return self.model.objects.annotate(log=F('table_b__log'),
                                               action=F('table_c__action')).\
                all()
    

    it's not taking the first element of the related table... but everything that is related.

    I haven't found the solution yet.

  2. Marc reporter

    Hi Maciej,

    Thank you for your response.

    I tried your suggestion, but it's sill the same.

            return self.model.objects.annotate(log=F('table_b__log'),
                                               action=F('table_c__action')).\
                distinct().\
                all()
    
            return self.model.objects.annotate(log=F('table_b__log'),
                                               action=F('table_c__action')).\
                all().\
                distinct()
    

    For a workaround I just created a new field 'log' and 'action' that updates everytime a change is done in the related table. I'm still trying to find a way.

  3. Maciej Wisniowski repo owner

    Issue is not directly related to django-datatables-view. It's a problem with creating proper query with Django ORM. Better to ask on django IRC or stack overflow. Closing.

  4. Log in to comment