Search code examples
djangodjango-mptt

Django Mptt : How to nest another Model


I have two Models, reseller and customer. I can generate Tree hierarchy for reseller, But I want to list down Customers under their immediate parent reseller.

models.py

from django.db import models
from mptt.models import MPTTModel, TreeForeignKey

# Create your models here.
class Reseller(MPTTModel):
    reseller_name = models.CharField(max_length=40)
    reseller_email = models.EmailField(max_length=70,blank=True)
    reseller_code = models.CharField(max_length=40)
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')

    class MPTTMeta:
        order_insertion_by = ['reseller_name']

    def __str__(self):
        return self.reseller_name


class Customer(models.Model):
    customer_name = models.CharField(max_length=40)
    customer_email = models.EmailField(max_length=70,blank=True)
    customer_code = models.CharField(max_length=40)
    reseller = models.ForeignKey(Reseller, on_delete=models.CASCADE, null=True, blank=True, related_name='cust_children')

    def __str__(self):
        return self.customer_name


This is the view :

views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import *
from .forms import *


# Create your views here.
def index(request):
    return render(request, 'main/login.html')

def home(request):
    resellers = Reseller.objects.all()
    customers = Customer.objects.all()
    context = { 'resellers' : resellers, 'customers' : customers }
    return render(request, 'main/home.html', context)

This the recursetree tag that is been cited in mptt docs. home.html

{% recursetree resellers %}
 <li>
  {{ node.reseller_name }} 
   {% if not node.is_leaf_node %}
     <ul class="children">
       {{ children }}
     </ul>    
   {% endif %}
  </li>
{% endrecursetree %}

Here is the order I'm aiming to:

reseller1
 -reseller2
 -customer1
 --reseller3
----customer2


Solution

  • The reason it doesn't work is because customers aren't part of the resellers tree, you need to get them manually. Something like this will give you a start:. If customers aren't always at a lead node you'll need to make some changes, though.

    {% recursetree resellers %}
      <li>
        {{ node.reseller_name }}
        {% if node.cust_children.all %}
          <ul>
            {% for customer in node.cust_children.all %}
              <li>{{ customer.customer_name }}</li>
            {% endfor %}
          </ul>
        {% endif %}
        {% if not node.is_leaf_node %}
          <ul class="children">
            {{ children }}
          </ul>
        {% endif %}
      </li>
    {% endrecursetree %}