Skip to content

Commit 88d0d97

Browse files
author
Maxime Toussaint
committed
Remove unused function, raise ValueError if prefetch_source is wrong value
1 parent ad9563f commit 88d0d97

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class SomeSerializer(PrefetchingSerializerMixin, serializer.ModelSerializer):
196196
)
197197
```
198198

199-
**IMPORTANT NOTE**: Because of an issue with Django, behaviour could be inconsistant when using to_attr with Prefetch objects. For more information, see: https://code.djangoproject.com/ticket/34791
199+
**IMPORTANT NOTE**: Because of an issue with Django, behaviour could be inconsistant when using to_attr with Prefetch objects. For more information, see: <https://code.djangoproject.com/ticket/34791>
200200

201201
----
202202

serializer_prefetch/base.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,14 @@ def _get_serializer_field_relations(
254254
if not is_prefetch_object and not is_model_field(model, source):
255255
if getattr(field, "_prefetch_source", None):
256256
raise ValueError(
257-
_('The field "{}" is not a model field.').format(source)
257+
_(
258+
'The prefetch_source "{}" is not a valid value for '
259+
"field {} on serializer {}."
260+
).format(
261+
source,
262+
field.source,
263+
serializer.__class__.__name__,
264+
)
258265
)
259266

260267
continue

serializer_prefetch/utils.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ def get_custom_related(related_attr, current_relation=None):
6464
return computed_related
6565

6666

67-
def transform_str_to_prefetch(item):
68-
if isinstance(item, str):
69-
return Prefetch(item)
70-
71-
return item
72-
73-
7467
def get_model_from_serializer(serializer):
7568
with suppress(AttributeError):
7669
return serializer.Meta.model

tests/test_conditions.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,19 @@ class Meta:
375375

376376
with self.assertNumQueries(1):
377377
serializer.data
378+
379+
# Passing a wrong prefetch_source should raise a ValueError
380+
class PizzaSerializer(PrefetchingSerializerMixin, serializers.ModelSerializer):
381+
provenance = CountrySerializer(
382+
source="provenance_", prefetch_source="wrong_source"
383+
)
384+
385+
class Meta:
386+
model = Pizza
387+
fields = ("label", "provenance")
388+
389+
pizzas = list(Pizza.objects.all())
390+
serializer = PizzaSerializer(pizzas, many=True)
391+
392+
with self.assertRaises(ValueError):
393+
serializer.data

0 commit comments

Comments
 (0)