0

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

sangramz
  • 3
  • 5
  • I'm not sure I follow. What's wrong with what you have? It should work as is, just set `Customer.parent` to the `Reseller` object you want. An aside, parent isn't a good name in `Customer` here, it'd be better to just call this `reseller`. – Tom Carrick May 07 '20 at 13:07
  • Tom, Thanks for responding, I changed the parent to reseller in customer. I'm not able to display customers under reseller when I run ``` recursetree ``` tag. It only gives hierarchies of Resellers. What query should I pass from view function to get all the customers under the resellers. – sangramz May 07 '20 at 14:07
  • Yes, because it's not part of the tree. If you show the recursetree code you have, it'll be possible to give a solution using that. – Tom Carrick May 07 '20 at 14:08
  • Tom, Just added the view and the recurse tag in my view. – sangramz May 07 '20 at 14:24
  • As an aside, don't use `import *`, it makes it impossible to know where things are coming from and can break code-completion and other features in your editor, as well as introducing subtle bugs. – Tom Carrick May 07 '20 at 14:57

1 Answers1

1

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 %}
Tom Carrick
  • 6,349
  • 13
  • 54
  • 78
  • Thanks, I have tried that earlier but it didn't worked. How can we make customer the part of the tree? – sangramz May 07 '20 at 14:44
  • 1
    It can't be done, but the code provided here should work. What's the problem when you try it? I've also noticed the customers can appear anywhere, so I've amended the code for this. – Tom Carrick May 07 '20 at 14:53
  • It shows the correct hierarchy of Reseller, But not customers, Though I have populated customers under various reseller. Reseller are perfectly displayed but not items from child class i.e. customers – sangramz May 07 '20 at 15:08
  • 1
    The latest code should show the hierarchy correctly I believe, unless I'm missing something. If they _do_ display, but not how you want them to, you'll need to give more details. – Tom Carrick May 07 '20 at 15:13
  • Now it's working all correctly. In my code I missed the related name "cust_children". Thank you very much for your time and efforts Tom. :) – sangramz May 07 '20 at 15:27
  • Don't forget to accept the answer if it's working for you. – Tom Carrick May 07 '20 at 17:34