Search through DateTimeField using String Characters in Django

Search through DateTimeField using String Characters in Django

standinibarrastandinibarra Posts: 15Questions: 5Answers: 0

Hi I have this DateTimeField in models.py

start_date = models.DateTimeField(default=timezone.now)

with output:
Date: 2021-02-19
Time: 15:49:05

And I am printing it in datatable by converting the date into strings and removing the time.
February 19, 2021

Now, the thing is I have a search textbox in the datatable and needed to search the date through using the converted string date.

I have this code as query search.

def query_custom_users_by_args(**kwargs):
    draw = int(kwargs.get('draw')[0])
    length = int(kwargs.get('length')[0])
    start = int(kwargs.get('start')[0])
    search_value = kwargs.get('search[value]')[0]
    order_column = kwargs.get('order[0][column]')[0]
    order = kwargs.get('order[0][dir]')[0]

    order_column = ORDER_COLUMN_CHOICES[order_column]
    if order == 'desc':
        order_column = '-' + order_column

    queryset = CustomUser.objects.exclude(username=F('email'))
    total = queryset.count()

    if search_value:
        queryset = queryset.filter(Q(id__icontains=search_value) |
                                   Q(username__icontains=search_value) |
                                   Q(email__icontains=search_value) |
                                   Q(palotype__icontains=search_value) |
                                   Q(suffix__icontains=search_value) |
                                   Q(first_name__icontains=search_value) |
                                   Q(middle_name__icontains=search_value) |
                                   Q(last_name__icontains=search_value) |
                                   Q(birth_date__icontains=search_value) |
                                   Q(region__icontains=search_value) |
                                   Q(city__icontains=search_value) |
                                   Q(barangay__icontains=search_value) |
                                   Q(unitno__icontains=search_value) |
                                   Q(floorno__icontains=search_value) |
                                   Q(bldgname__icontains=search_value) |
                                   Q(housebldgno__icontains=search_value) |
                                   Q(streetname__icontains=search_value) |
                                   Q(villagedistrict__icontains=search_value) |
                                   Q(mobileno__icontains=search_value) |
                                   Q(landline__icontains=search_value) |
                                   Q(start_date=search_value))


    count = queryset.count()
    queryset = queryset.order_by(order_column)[start:start + length]
    return {
        'items': queryset,
        'count': count,
        'total': total,
        'draw': draw
    }

And yah the Q(start_date=search_value)) is causing an error.

Another reference to the codes is here.

send help!!!

Answers

  • standinibarrastandinibarra Posts: 15Questions: 5Answers: 0

    I'll give up on searching through converted date string but is there anyway to search the date '2021-02-19' with just using '__icontains'?

    like I'll search for 2021-19

  • kthorngrenkthorngren Posts: 21,262Questions: 26Answers: 4,934

    And yah the Q(start_date=search_value)) is causing an error.

    Did you look at the Ajax response as documented in the troubleshooting steps of the technote https://datatables.net/manual/tech-notes/7 ? Likely you will need to look at your Django debugs to find the error.

    Are you trying to create a server side processing Django script? I suggest you use client side processing if possible and let Datatables perform searching, sorting and paging in the client. See this FAQ for details. If you need server side processing there are some third party Django libraries that support Datatables server side processing.

    Kevin

  • standinibarrastandinibarra Posts: 15Questions: 5Answers: 0

    Hi Kevin, I'm so busy that I've forgot this question of mine. Anyway, what I did to solved the problem is that I created another field for date which only contains string like February 19, 2021 and then used the Q(start_date=search_value)). Anyway, yes bro I'm using server side process adviced by my friends coz before I'm just using django loop to insert the data in the datatable but they say 'what If I reach a 100k or more records, it might get lag and slow the web so I followed their advice, but server side is too complicated, there's so many to adjust and it's so stressful :(

    so if client side works like u says, I might replace my codes again. It may took some time again but I guess that's more good than serverside. I'll follow ur advice

  • standinibarrastandinibarra Posts: 15Questions: 5Answers: 0

    I just posted a comment but it somehow doesn't posted lmao anyway, i'll stick for server-side for now, I tried client-side with 50k records but it's laggy to the point it duplicates the datatables_info

  • standinibarrastandinibarra Posts: 15Questions: 5Answers: 0

    I only have one problem in server-side, the thing is I added a download button in the datatable settings. It works but it only download the data shown in the datatable, what I want is to download all the data loaded in the datable not just the shown data, is it possible?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    This thread should help, it's asking the same thing - i.e. exporting all data with serverSide.

    Cheers,

    Colin

  • lexannlexann Posts: 3Questions: 1Answers: 0

    Have you tried using Django serializers? They're simplifying a lot the use of a DateTimeField. They allow custom formatting for each model's DateTimeField, so you can send the datetime field in the required format to display by datatables.
    Another thing is, using '__icontains' on date looks pretty odd to me, unless for some reason datetime is stored in the database as a string. For dates, more appropriate search is by some sort of '__gt' or else, but I guess that in this case you should rewrite your query to first define if you're searching by a date.
    Also, consider using this answer to shorten your query.
    Hope this helps,
    Alexandra

This discussion has been closed.