Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

django .objects.values_list how to exlude None value

I’m using django .objects.values_list to get all the values of a filed of Model:

 def gen_choice(filed):
        return list(Mymodel.objects.values_list(filed, flat=True).distinct())

I want to exclude all the None value in the above query set :

Mymodel.objects.values_list(filed, flat=True).distinct()

Or the list:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

list(Mymodel.objects.values_list(filed, flat=True).distinct())

I have tried:

 def gen_choice(filed):
        return list(Mymodel.objects.exclude(filed=None).values_list(filed, flat=True).distinct())

Error:

django.core.exceptions.FieldError: Cannot resolve keyword 'filed' into field. Choices are: 

>Solution :

You can pass this as kwargs, so:

def gen_choice(flield):
    return list(
        Mymodel.objects.exclude(**{field: None})
        .values_list(field, flat=True)
        .distinct()
    )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading