Skip to content

Commit 840e380

Browse files
committed
Fix single related model access when predicate contains a primitive
1 parent 48c86bc commit 840e380

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

relativity/fields.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,11 @@ def get_extra_restriction(self, where_class, alias, related_alias):
242242
def _resolve_expression_local_references(cls, expr, obj):
243243
if isinstance(expr, L):
244244
return expr._relativity_resolve_for_instance(obj)
245-
else:
245+
elif hasattr(expr, "get_source_expressions"):
246246
for source_expr in expr.get_source_expressions():
247247
cls._resolve_expression_local_references(source_expr, obj)
248+
else:
249+
return expr
248250
return expr
249251

250252
def get_forward_related_filter(self, obj):

tests/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class Product(models.Model):
120120
colour = models.CharField(max_length=20)
121121
shape = models.CharField(max_length=20)
122122
size = models.IntegerField()
123+
deleted = models.BooleanField(default=False)
123124

124125
def __str__(self):
125126
return "Product #%s: a %s %s, size %s" % (
@@ -136,7 +137,7 @@ class CartItem(models.Model):
136137

137138
product = Relationship(
138139
Product,
139-
Q(sku=L("product_code")),
140+
Q(deleted=False, sku=L("product_code")),
140141
related_name="cart_items",
141142
multiple=False,
142143
null=False,

tests/tests.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ def setUpTestData(cls):
9090
Product(pk=6, sku="66", size=5, colour="blue", shape="square"),
9191
Product(pk=7, sku="77", size=1, colour="yellow", shape="circle"),
9292
Product(pk=8, sku="88", size=2, colour="green", shape="triangle"),
93-
Product(pk=9, sku="99", size=2, colour="red", shape="square"),
93+
Product(
94+
pk=9, sku="99", size=2, colour="red", shape="square", deleted=True
95+
),
9496
]
9597
)
9698

@@ -366,7 +368,7 @@ def test_not_nullable(self):
366368
item = CartItem.objects.create(
367369
pk=4,
368370
product_code="nonexistent",
369-
description="cart item for anonexistent product",
371+
description="cart item for a non-existent product",
370372
)
371373
with self.assertRaises(Product.DoesNotExist):
372374
item.product
@@ -382,3 +384,13 @@ def test_descriptor_not_cached(self):
382384
item.product.delete()
383385
with self.assertRaises(Product.DoesNotExist):
384386
item.product
387+
388+
def test_primitive_in_predicate(self):
389+
product = Product.objects.filter(deleted=True).first()
390+
item = CartItem.objects.create(
391+
pk=5,
392+
product_code=product.sku,
393+
description="cart item for a deleted product",
394+
)
395+
with self.assertRaises(Product.DoesNotExist):
396+
item.product

0 commit comments

Comments
 (0)