Tim
Tim

Reputation: 13

name 'field_name' is not defined error while querying the db

I am trying to query a foreign key attribute i am getting error as name 'field_name' is not defined

Mymodel.objects.filter(field_name)

Upvotes: 1

Views: 478

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477200

It makes no sense to say filter(field_name): a filter means that a field is the same, less than, contains, etc. some value, or the value of some other field.

You can for example filter with:

MyModel.objects.filter(field_name=some_value)

or check if it is not null with:

MyModel.objects.filter(field_name__isnull=False)

if field_name is a ForeignKey, we will thus only retain MyModels where the ForeignKey does not point to None/NULL.

Upvotes: 1

Related Questions