Search code examples
pythondjangodjango-modelsdjango-viewsdjango-orm

Retrieve the same values ​whose data is there or exists and not the rest.In django



I want to have a data that does not have any empty value in database or in row. I wrote like this in my code.

faq = FAQ.objects.values('question','answer','field_id')

this the output in my terminal

{'question': None, 'answer': None, 'field_id': None}
{'question': 'Test question', 'answer': '<p>Testsaddsf description</p>\r\n', 'field_id': 'TestTest'}

i don't want None value data.


Solution

  • You can filter with the __isnull lookup [Django-doc]:

    faq = FAQ.objects.filter(
        question__isnull=False,
        answer__isnull=False,
        field_id__isnull=False
    ).values('question','answer','field_id')

    This will thus only include records where none of question, answer or field_id are NULL/None.