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.

Advertisements

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s