1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #34443 -- Fixed filtering by transforms on reverse relations.

Regression in ce6230aa97.
This commit is contained in:
Mariusz Felisiak 2023-03-28 21:03:24 +02:00 committed by GitHub
parent 3afdc9e9b4
commit 996c802229
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View File

@ -114,6 +114,12 @@ class ForeignObjectRel(FieldCacheMixin):
def get_lookup(self, lookup_name): def get_lookup(self, lookup_name):
return self.field.get_lookup(lookup_name) return self.field.get_lookup(lookup_name)
def get_lookups(self):
return self.field.get_lookups()
def get_transform(self, name):
return self.field.get_transform(name)
def get_internal_type(self): def get_internal_type(self):
return self.field.get_internal_type() return self.field.get_internal_type()

View File

@ -19,7 +19,7 @@ from django.db.models import (
Value, Value,
When, When,
) )
from django.db.models.functions import Cast, Length, Substr from django.db.models.functions import Abs, Cast, Length, Substr
from django.db.models.lookups import ( from django.db.models.lookups import (
Exact, Exact,
GreaterThan, GreaterThan,
@ -838,6 +838,31 @@ class LookupTests(TestCase):
with self.assertRaisesMessage(FieldError, msg): with self.assertRaisesMessage(FieldError, msg):
Tag.objects.filter(articles__foo="bar") Tag.objects.filter(articles__foo="bar")
def test_unsupported_lookup_reverse_foreign_key(self):
msg = (
"Unsupported lookup 'title' for ManyToOneRel or join on the field not "
"permitted."
)
with self.assertRaisesMessage(FieldError, msg):
Author.objects.filter(article__title="Article 1")
def test_unsupported_lookup_reverse_foreign_key_custom_lookups(self):
msg = (
"Unsupported lookup 'abspl' for ManyToOneRel or join on the field not "
"permitted, perhaps you meant abspk?"
)
fk_field = Article._meta.get_field("author")
with self.assertRaisesMessage(FieldError, msg):
with register_lookup(fk_field, Abs, lookup_name="abspk"):
Author.objects.filter(article__abspl=2)
def test_filter_by_reverse_related_field_transform(self):
fk_field = Article._meta.get_field("author")
with register_lookup(fk_field, Abs):
self.assertSequenceEqual(
Author.objects.filter(article__abs=self.a1.pk), [self.au1]
)
def test_regex(self): def test_regex(self):
# Create some articles with a bit more interesting headlines for # Create some articles with a bit more interesting headlines for
# testing field lookups. # testing field lookups.