Create Hierarchical Category Tree In Django

I have a large CSV (27k + rows) that I want to import into a model and from there create a hierarchical tree in a template.

Example

Hierarchical Example

The child categories go up to 9 levels deep.

I was thinking MPTT would maybe be the way to go because of the size of the dataset, but the tutorial doesn't talk about large imports and I can't extrapolate how to use their example in a large set.

I'm not married to mptt, I'd be fine even doing this with multiple tables and somehow relate them like a many to one.

That brings me to my question:

How do you relate the child to the parent in a large set you can't hand type in?

Category Screenshot

category screenshot

mptt tutorial example

from myapp.models import Genre

rock = Genre.objects.create(name="Rock")
blues = Genre.objects.create(name="Blues")
Genre.objects.create(name="Hard Rock", parent=rock)
Genre.objects.create(name="Pop Rock", parent=rock)

Things I've Tried

I know this is wrong on multiple levels. That is not a good use of time/space on a large set.

def show_categories(request):
    categories = Categories.objects.all()
    parents = {}
    children = {}
    for cat in categories:
        if cat.cat_parent_id == 0:
          parents[cat.cat_name] = cat.category_id
        if cat.cat_parent_id > 0:
          children[cat.cat_name] = cat.cat_parent_id
    return render(
      request,
      "amazon_niche_finder/categories.html",
      {
          "parents": parents,
          "children": children,
      },
    )  

Here's my import. I've tried variations of the above code with this CSV import instead of in the view, but no luck.

with open(path, "rt") as f:
        reader = csv.reader(f, dialect="excel")
        next(reader)
        Categories.objects.bulk_create([
            Categories(
                category_id=int(row[0]),
                cat_name=row[1],
                cat_parent_id=int(row[2]),
                cat_level=int(row[3]),
                cat_link=row[4],
            ) for row in reader
        ])

Model

class Categories(models.Model):
cat_id = models.IntegerField(null=False, name="category_id")
cat_name = models.CharField(max_length=100,
                            null=False,
                            name="cat_name",
                            default="")
cat_parent_id = models.IntegerField(null=False, name="cat_parent_id")
cat_level = models.IntegerField(null=False, name="cat_level")
cat_link = models.URLField(null=True, name="cat_link")

My biggest issue is linking the children to the parent. Specifically how do I say "if parent_id == 1 then put me under cat_id 1"?

Back to Top