Skip to content

Commit 44930b1

Browse files
committed
Remove excluding many-to-many functionality because handling this with prefetch is preferable.
1 parent 5c3e98f commit 44930b1

File tree

2 files changed

+5
-65
lines changed

2 files changed

+5
-65
lines changed

django_unicorn/serializer.py

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,7 @@ def _get_model_dict(model: Model) -> dict:
122122
model_json = model_json.get("fields")
123123
model_json["pk"] = model_pk
124124

125-
exclude_field_related_names = getattr(
126-
model, "__unicorn__exclude_field_related_names", []
127-
)
128-
129125
for related_name in _get_many_to_many_field_related_names(model):
130-
if related_name in exclude_field_related_names:
131-
continue
132-
133126
pks = []
134127

135128
try:
@@ -282,58 +275,6 @@ def _exclude_field_attributes(
282275
del dict_data[field_name][field_attr]
283276

284277

285-
def _handle_many_to_many_excluded_field_attributes(
286-
data: Dict, exclude_field_attributes: Optional[Tuple[str]]
287-
) -> Optional[Tuple[str]]:
288-
"""
289-
Explicitly handle excluding many-to-many fields on models with a semi-hacky private
290-
`__unicorn__exclude_field_related_names attribute that gets used later in `_get_model_json`.
291-
Since the many-to-many field won't be serialized, remove it from the list so it won't
292-
be tried to be removed in `_exclude_field_attributes`.
293-
"""
294-
295-
if exclude_field_attributes:
296-
many_to_many_field_attributes = set()
297-
298-
for field_attributes in exclude_field_attributes:
299-
if "." not in field_attributes:
300-
continue
301-
302-
(field_attribute, exclude_field_related_name, *_) = field_attributes.split(
303-
"."
304-
)
305-
306-
for key in data.keys():
307-
if isinstance(data[key], Model) and key == field_attribute:
308-
model = data[key]
309-
310-
many_to_many_related_names = _get_many_to_many_field_related_names(
311-
model
312-
)
313-
314-
if exclude_field_related_name in many_to_many_related_names:
315-
if hasattr(model, "__unicorn__exclude_field_related_names"):
316-
model.__unicorn__exclude_field_related_names.append(
317-
exclude_field_related_name
318-
)
319-
else:
320-
setattr(
321-
model,
322-
"__unicorn__exclude_field_related_names",
323-
[exclude_field_related_name],
324-
)
325-
326-
many_to_many_field_attributes.add(field_attributes)
327-
break
328-
329-
# Convert list to tuple again so it's hashable for `lru_cache`
330-
exclude_field_attributes = tuple(
331-
set(exclude_field_attributes) - many_to_many_field_attributes
332-
)
333-
334-
return exclude_field_attributes
335-
336-
337278
def dumps(
338279
data: Dict, fix_floats: bool = True, exclude_field_attributes: Tuple[str] = None
339280
) -> str:
@@ -356,10 +297,6 @@ def dumps(
356297
exclude_field_attributes
357298
), "exclude_field_attributes type needs to be a sequence"
358299

359-
exclude_field_attributes = _handle_many_to_many_excluded_field_attributes(
360-
data, exclude_field_attributes
361-
)
362-
363300
serialized_data = orjson.dumps(data, default=_json_serializer)
364301

365302
if fix_floats:

tests/serializer/test_dumps.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,11 @@ def test_model_many_to_many_with_excludes(django_assert_num_queries):
250250
flavor_one.taste_set.add(taste2)
251251
flavor_one.taste_set.add(taste3)
252252

253-
# This shouldn't make any database calls because the many-to-manys are excluded and
254-
# all of the other data is already set
253+
flavor_one = Flavor.objects.prefetch_related("taste_set", "origins").get(
254+
pk=flavor_one.pk
255+
)
256+
257+
# This shouldn't make any database calls because of the prefetch_related
255258
with django_assert_num_queries(0):
256259
actual = serializer.dumps(
257260
{"flavor": flavor_one},

0 commit comments

Comments
 (0)