Fixed object names
This commit is contained in:
parent
1545ca9035
commit
04bada1cd1
|
|
@ -112,19 +112,22 @@ class Collection(ProjectBaseMixin, CollectionDateMixin,
|
||||||
@property
|
@property
|
||||||
def related_object_names(self) -> list:
|
def related_object_names(self) -> list:
|
||||||
"""Return related object names."""
|
"""Return related object names."""
|
||||||
raw_object_names = {}
|
raw_objects = []
|
||||||
for related_object in [(related_object.id, related_object.name) for related_object in self._related_objects]:
|
for related_object in [related_object.name for related_object in self._related_objects]:
|
||||||
instances = getattr(self, f'{related_object[1]}')
|
instances = getattr(self, f'{related_object}')
|
||||||
if instances.exists():
|
if instances.exists():
|
||||||
for instance in instances.all():
|
for instance in instances.all():
|
||||||
raw_object_names[related_object[0]] = instance.slug if hasattr(instance, 'slug') else None
|
raw_object = (instance.id, instance.slug) if hasattr(instance, 'slug') else (
|
||||||
|
instance.id, None
|
||||||
|
)
|
||||||
|
raw_objects.append(raw_object)
|
||||||
|
|
||||||
# parse slugs
|
# parse slugs
|
||||||
related_objects = []
|
related_objects = []
|
||||||
object_names = set()
|
object_names = set()
|
||||||
re_pattern = r'[\w]+'
|
re_pattern = r'[\w]+'
|
||||||
for object_id in raw_object_names:
|
for object_id, raw_name, in raw_objects:
|
||||||
result = re.findall(re_pattern, raw_object_names[object_id])
|
result = re.findall(re_pattern, raw_name)
|
||||||
if result:
|
if result:
|
||||||
name = ' '.join(result).capitalize()
|
name = ' '.join(result).capitalize()
|
||||||
if name not in object_names:
|
if name not in object_names:
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ class CollectionBackOfficeSerializer(CollectionBaseSerializer):
|
||||||
source='get_collection_type_display', read_only=True)
|
source='get_collection_type_display', read_only=True)
|
||||||
country = CountrySimpleSerializer(read_only=True)
|
country = CountrySimpleSerializer(read_only=True)
|
||||||
count_related_objects = serializers.IntegerField(read_only=True)
|
count_related_objects = serializers.IntegerField(read_only=True)
|
||||||
related_object_names = serializers.JSONField(read_only=True)
|
related_object_names = serializers.ListField(read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Collection
|
model = models.Collection
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user