Added related_object_ids
This commit is contained in:
parent
5c171c644c
commit
8f6dd7e5c1
|
|
@ -24,7 +24,8 @@ class CollectionNameMixin(models.Model):
|
|||
|
||||
class CollectionDateMixin(models.Model):
|
||||
"""CollectionDate mixin"""
|
||||
start = models.DateTimeField(_('start'))
|
||||
start = models.DateTimeField(blank=True, null=True, default=None,
|
||||
verbose_name=_('start'))
|
||||
end = models.DateTimeField(blank=True, null=True, default=None,
|
||||
verbose_name=_('end'))
|
||||
|
||||
|
|
@ -110,20 +111,29 @@ class Collection(ProjectBaseMixin, CollectionDateMixin,
|
|||
@property
|
||||
def related_object_names(self) -> list:
|
||||
"""Return related object names."""
|
||||
raw_object_names = []
|
||||
for related_object in [related_object.name for related_object in self._related_objects]:
|
||||
instances = getattr(self, f'{related_object}')
|
||||
raw_object_names = {}
|
||||
for related_object in [(related_object.id, related_object.name) for related_object in self._related_objects]:
|
||||
instances = getattr(self, f'{related_object[1]}')
|
||||
if instances.exists():
|
||||
for instance in instances.all():
|
||||
raw_object_names.append(instance.slug if hasattr(instance, 'slug') else None)
|
||||
raw_object_names[related_object[0]] = instance.slug if hasattr(instance, 'slug') else None
|
||||
|
||||
# parse slugs
|
||||
object_names = []
|
||||
related_objects = []
|
||||
object_names = set()
|
||||
re_pattern = r'[\w]+'
|
||||
for raw_name in raw_object_names:
|
||||
result = re.findall(re_pattern, raw_name)
|
||||
if result: object_names.append(' '.join(result).capitalize())
|
||||
return set(object_names)
|
||||
for object_id in raw_object_names:
|
||||
result = re.findall(re_pattern, raw_object_names[object_id])
|
||||
if result:
|
||||
name = ' '.join(result).capitalize()
|
||||
if name not in object_names:
|
||||
related_objects.append({
|
||||
'id': object_id,
|
||||
'name': name
|
||||
})
|
||||
object_names.add(name)
|
||||
|
||||
return related_objects
|
||||
|
||||
|
||||
class GuideTypeQuerySet(models.QuerySet):
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user