[Answered ]-How to use Django ORM to find dangling records?

1👍

You can .filter(…) [Django-doc] with:

Entity.objects.filter(apple=None, orange=None, banana=None)

This will make LEFT OUTER JOINs on the tables for the Apple, Orange, and Banana models, and then check if these are None/NULL.

It will work with the value related_query_name=… parameter [Django-doc] for the ForeignKeys from Apple, Orange and Banana to Entity. If that one is not specified, it will use the related_name=… parameter [Django-doc] instead, and if that is not specified either, it uses the name of the model in lowercase, so here apple, orange and banana.

Leave a comment