﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
11319	ForeignKey filters use the wrong field to prepare values for database	Russell Keith-Magee	Carl Meyer	"There are a couple of edge cases where filters on foreign keys can fail, as a result of a deep-seated problem with the way get_db_prep_value() is used.

If Model A has a foreign key pointing to B, a query of A.objects.filter(b=b_instance) will use the primary key on A to prepare the value for the database. 

If both models have the same primary key type, this poses no problem.

However if:
 1. the two fields have a different get_db_prep_value() implementation (usually caused by having primary keys that are of substantially different types), or
 2. the foreign key specifies a to_field argument

then the value prepared for database lookup will be wrong.

Attached patch describes regression tests for this problem.

Related tickets:
 * #10243 papered over this problem for the admin
 * #10785 originally reported this problem for a small subset of cases (when the PK is a custom field)"		closed	Database layer (models, ORM)	1.0		fixed		kmishler@… Carsten Fuchs Andrey Golovizin	Accepted	1	0	0	1	0	0
