django prefetch_related and select_related on parent table

Viewed 963

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 fetch all categories and their parent.

If I do:

Category.objects.select_related("parent").filter(pk__in=[ids])

it would just return the parent of the first level.

How can I retrieve the parents of all level with minimum db calls?

My approach is to build a new non-db-model objects e.g: CategorySerializer that will transfer those Category models to non db ones so the logic layer could use it

1 Answers
Related