Reputation: 13
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
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 MyModel
s where the ForeignKey
does not point to None
/NULL
.
Upvotes: 1