Django — How to filter models by a field on the model

I had read this functionality in the docs, but couldn’t find it with my googlefu.

Update: I couldn’t find my own post via my googlefu when I needed it again so I will insert some useful keywords here: Django Self Reference Field

For future reference, it’s the F object from django.db.models

Syntax:

Model.objects.filter(some_field=F('other_field'))

This would filter for models where some_field is equal to other_field in the same instance.

1 Comment

  1. Omar says:

    Excelent :), thats was very useful for me !!!

Leave a reply to Omar Cancel reply