user10332687
user10332687

Reputation:

Filtering on foreign key in django

In django, why does it not allow:

ItemInstance.objects.filter(provider_id__icontains='sting'))

But it does allow:

ItemInstance.objects.filter(provider__name__icontains='sting'))

provider_id and provider__name would give the same value, as the foreign key is to the name field. Why doesn't it allow the first method of referencing it?

Upvotes: 0

Views: 92

Answers (1)

Rich Tier
Rich Tier

Reputation: 9461

I suspect you're using a natural key as the primary key: you've got primary=True on the name field? If so use this:

ItemInstance.objects.filter(provider_pk__icontains='sting'))

id field is only present if you don't specify your primary key. Django accommodates this my providing pk, which is a proxy for whatever field is your primary key

Upvotes: 1

Related Questions