4

In the search form field, If I don't enter anything, but push on the button to search the query. The query returns every each field from the database. Company.objects.exclude(Q(name__isnull=True)).exclude(Q(name__exact='')) Somehow, doesn't help me. If I use them together or separate. The code in views.py is:

class className(listView)
    model=Company
    template_name='name_of_the_template.html'

    def get_queryset(self):
        query=self.request.Get.get('q')
        query=query.replace('#',' ') //same for ?\/$%^+*()[] *tried to set exclude code here

        if query == ' ':
            object_list = Company.objects.filter(Q(name__icontains='None'))//sending to the empty object
            return object_list
    else:
        // *tried to set exclude code here
            object_list = Company.objects.filter(Q(name__icontains=query))
            return object_list

None of exclude code helps. I believe I do simple mistake somewhere... because code suppose to work, but somehow still. I just also tried to get rid off if else statement and use exclude statements for queries such as '', ' ' and null and after that filter for query... And now excluding queries for ' ' also doesn't work...

Django doesn't tell me the exclude doesn't work, all works, except, it is like not in the code.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
bugthefifth
  • 161
  • 15
  • 1
    Exactly what do yout to happen if the input is empty? – Willem Van Onsem Apr 22 '20 at 16:38
  • just would like it return nothing or query emty object, which is under name 'None' or replace '' with ' ' like it does with #... and then I send ' ' query to empty object. – bugthefifth Apr 22 '20 at 16:42
  • if I try to query=query.replace('', ' ') then django gives me mistake – bugthefifth Apr 22 '20 at 16:44
  • if I set space once in the search field, the code works and sends user to empty object, but if I don't enter anything in the search field, but just push "search", it gives me every field from database... – bugthefifth Apr 22 '20 at 16:48
  • yes because then query is `''` (the empty string), and thus `else` will be triggered, and each element "contains" the empty string. – Willem Van Onsem Apr 22 '20 at 16:50

1 Answers1

1

If you don't enter anything, then the string is empty '', or it is None if no parameter is passed. You can thus check the truthiness of query, you probably however better first .strip() the string to remove any leading or trailing spaces:

def get_queryset(self):
    query = self.request.Get.get('q')
    if query is not None:
        query = query.replace('#',' ')
        query = query.strip()
    if not query:
        return Company.objects.none()
    else:
        return Company.objects.filter(name__icontains=query)

In the template, you can then use a {% for … %}…{% empty %}…{% endfor %} template block [Django-doc]:

{% for object in object_list %}
    {{ object }}
{% empty %}
    no objects found!
{% endfor %}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • thank you... if you don't mind... I saw everybody advise to use exclude code, and exact code... why it didn't work for me? – bugthefifth Apr 22 '20 at 16:51
  • @bugthefifth: where did you see that advice? `.exclude()` is used to filter *out* elements that satisfy the condition (just like `.filter(..)` is used to filter *in* all elements that satisfy the condition). – Willem Van Onsem Apr 22 '20 at 16:53
  • https://stackoverflow.com/questions/844556/filtering-for-empty-or-null-names-in-a-queryset – bugthefifth Apr 22 '20 at 16:55
  • @bugthefifth: but that means if you want to return values for which `name` *is* `NULL`, or `name` *is* the empty string `''`, or as in the question, filter these out. – Willem Van Onsem Apr 22 '20 at 16:56
  • may be I didn't understand something... but I went through couple of other places, same advices... again, may be I am mistaken for some reason? and I ddin't look for the right answer... – bugthefifth Apr 22 '20 at 16:58
  • I see... Sure I knew that something in my understanding is off, just make sure that I completely understand it now. Thank you! – bugthefifth Apr 22 '20 at 17:01
  • 1
    Willem, thank you for the complete and efficient answer! – bugthefifth Apr 22 '20 at 17:15