I have the following db structure:
catogory
id
name
parent_id
class Category(models.Model)
name = models.CharField(max_length=400, blank=True, null=True)
parent = models.ForeignKey("self", blank=True, null=True)
I need to get all categories and their parent.
If I do this:
Category.objects.select_related("parent").filter(pk__in=[ids])
it will just return the parent of the first level.
How can I get parents of all levels with minimal db calls?
My approach is to create new objects of non-db models, for example: CategorySerializer, which will transfer these category models to non-db so that the logical level can use it
source
share