I have the following code that iterates the tags queryset, and for each item, creates a Department object and adds it to the departments list:
departments: List[Department] = []
tags = Tag.objects.filter(type="department")
for tag in tags:
dept_id = tag.reference_id
dept_name = tag.name
parent_tag = Tag.objects.get(type="department", reference_id=tag.parent_reference_id)
dept_parent_id = parent_tag.reference_id
departments.append(Department(dept_id, dept_name, dept_parent_id))
However, as you can see, it is making multiple DB calls via Tag.objects.get(), which seems highly inefficient. Is there an efficient way to populate that departments list without making so many DB calls?
TIA.