Filtering on a relation whose model has a different PK type doesn't work
|Reported by:||David Gouldin||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.2|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Using the following models:
class Author(models.Model): name = models.CharField(max_length=255) class Book(models.Model): name = models.CharField(max_length=50, primary_key=True) author = models.ForeignKey(Author)
The following code fails:
In : a = Author.objects.create(name='Me') In : b = Book.objects.create(name='My Book', author=a) In : Author.objects.filter(book=b) .... ValueError: invalid literal for int() with base 10: 'My Book'
This seems to be the source of the problem:
_pk_trace uses field.rel.to to resolve the field to use for prepping the filter value. In the case above, It's preparing Book.pk (a CharField) as if it were a value for Author.pk (an AutoField) and blows up when casting the string to an int. I'm not sure how best to go about solving the issue. _pk_trace doesn't currently have any way I know of to tell which side of the relation the value arg is coming from.
Change History (7)
comment:3 Changed 5 years ago by
|Status:||closed → reopened|