[Django]-How to include "None" in lte/gte comparisons?

5👍

✅

Most database engines don’t like relational comparisons with NULL values. Use <field>__isnull to explicitly check if a value is NULL in the database, but you’ll need to use Q objects to OR the conditions together.

17👍

Flip the logic and use exclude(). What you really want to do is exclude any data that specifies a date that doesn’t fit. If pickup_latest and pickup_earliest are NULL it shouldn’t match the exclude query and wont be removed. Eg

exclude['pickup_latest__lt'] = d['available_on']
exclude['pickup_earliest__gt'] = d['available_on']

0👍

Don’t think that’s actually a django-specific question. Variable ‘d’ is a python dictionary, no? If so, you can use this:

filter['pickup_latest__gte'] = d.get('available_on', None)

Leave a comment