perf: 优化 json m2m field

pull/10327/head
ibuler 2023-04-27 18:05:16 +08:00
parent 90090a7fc7
commit 5a6e13721d
1 changed files with 11 additions and 2 deletions

View File

@ -220,8 +220,17 @@ class PhoneField(serializers.CharField):
class JSONManyToManyField(serializers.JSONField):
def to_representation(self, value):
return value.value
def to_representation(self, manager):
if manager is None:
return manager
value = manager.value
if not isinstance(value, dict):
return {"type": "ids", "ids": []}
if value.get("type") == "ids":
valid_ids = manager.all().values_list("id", flat=True)
valid_ids = [str(i) for i in valid_ids]
return {"type": "ids", "ids": valid_ids}
return value
def to_internal_value(self, data):
if not data: