0

I have two models for two parts of AC, that is IndoorInventory for the indoor unit which shows all indoor unit information, and OutdoorInventory for showing outdoor unit information.

class IndoorInventory(models.Model):
    client = models.ForeignKey(Client, on_delete=models.CASCADE)
    product = models.CharField(max_length=50)
    indoor_brand = models.CharField(max_length=100, null=True, blank=True)
    indoor_model_no = models.CharField(max_length=100, null=True, blank=True)
    indoor_sr_no = models.CharField(max_length=100, null=True, blank=True)
    outdoor_model_no = models.CharField(max_length=100, null=True, blank=True)
class OutdoorInventory(models.Model):
    client = models.ForeignKey(Client, on_delete=models.CASCADE)
    product = models.CharField(max_length=50)
    outdoor_brand = models.CharField(max_length=100, null=True, blank=True)
    outdoor_model_no= models.CharField(max_length=100, null=True, blank=True)
    outdoor_sr_no = models.CharField(max_length=100, null=True, blank=True)
    indoor_model_no= models.CharField(max_length=100, null=True, blank=True)

I am passing a queryset of IndoorInventory to my Django template and fetching all the data there. Below is my VIEW

def clientInventory(request):
    context = {}
    client_id = request.GET.get('client_id')

    id_inven = IndoorInventory.objects.filter(client=client_id)
    od_inven = OutdoorInventory.objects.filter(client=client_id)
    
    context['id_inven'] = id_inven
    return render(request, 'client-inventory.html', context)

My Template Below

<table id="client-invent-admin" class="table table-striped table-bordered display nowrap">
                                <thead>
                                <tr>
                                    <th class="filterhead">ID</th>
                                    <th class="filterhead">Brand</th>
                                    <th class="filterhead">Product/Type</th>
                                    <th class="filterhead">ID Model No</th>
                                    <th class="filterhead">ID SR No</th>
                                    <th class="filterhead">OD Model No</th>
                                    <th class="filterhead">Actions</th>
                                </tr>
                                <tr>
                                    <th>ID</th>
                                    <th>Brand</th>
                                    <th>Product/Type</th>
                                    <th>ID Model No</th>
                                    <th>ID SR No</th>
                                    <th>OD Model No</th>
                                    <th>Actions</th>
                                </tr>
                                </thead>

                                <tbody>
                                {% for invent in id_inven %}
                                {% if invent.indoor_model_no == '' %}
                                <tr class="bg-danger">
                                    <td>{{ invent.id }}</td>
                                    <td>{{ invent.indoor_brand }}</td>
                                    <td>{{ invent.product }}</td>
                                    <td>{{ invent.indoor_model_no }}</td>
                                    <td>{{ invent.indoor_sr_no }}</td>
                                    <td>{{ invent.outdoor_model_no }}</td>
                                    <td>
                                        <div class="dropdown icon-dropdown">
                                            <a class="btn-icon dropdown-toggle" data-toggle="dropdown" href="#">
                                                <i class="zmdi zmdi-more"></i>
                                            </a>
                                            <div class="dropdown-menu dropdown-menu-right">
                                                <a class="dropdown-item" href="#"><i class="feather icon-eye"></i> View</a>
                                                <div class="dropdown-divider"></div>

                                                <a class="dropdown-item" href="#"><i class="feather icon-edit"></i> Edit</a>
                                                <div class="dropdown-divider"></div>

                                                <a class="dropdown-item"
                                                   href="/delClient-invent/?invent_id={{invent.id}}&client_id={{client_id}}">
                                                    <i class="feather icon-trash"></i> Delete
                                                </a>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                {% else %}
                                <tr>
                                    <td>{{ invent.id }}</td>
                                    <td>{{ invent.indoor_brand }}</td>
                                    <td>{{ invent.product }}</td>
                                    <td>{{ invent.indoor_model_no }}</td>
                                    <td>{{ invent.indoor_sr_no }}</td>
                                    <td>{{ invent.outdoor_model_no }}</td>
                                    <td>
                                        <div class="dropdown icon-dropdown">
                                            <a class="btn-icon dropdown-toggle" data-toggle="dropdown" href="#">
                                                <i class="zmdi zmdi-more"></i>
                                            </a>
                                            <div class="dropdown-menu dropdown-menu-right">
                                                <a class="dropdown-item" href="#"><i class="feather icon-eye"></i> View</a>
                                                <div class="dropdown-divider"></div>

                                                <a class="dropdown-item" href="#"><i class="feather icon-edit"></i> Edit</a>
                                                <div class="dropdown-divider"></div>

                                                <a class="dropdown-item"
                                                   href="/delClient-invent/?invent_id={{invent.id}}&client_id={{client_id}}">
                                                    <i class="feather icon-trash"></i> Delete
                                                </a>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                {% endif %}
                                {% endfor %}
                                </tbody>

                                <tfoot>
                                <tr>
                                    <th>ID</th>
                                    <th>Brand</th>
                                    <th>Product/Type</th>
                                    <th>ID Model No</th>
                                    <th>ID SR No</th>
                                    <th>OD Model No</th>
                                    <th>Actions</th>
                                </tr>
                                </tfoot>
                            </table>

See table frontend

Now I want to get the outdoor_sr_no from OutdoorInventory models and pass it to the IndoorInventory Queryset so that the outdoor_sr_no value also get fetched on the table next to (OD model no) column in the table

2 Answers2

0

You can use the chain method which concatenates the querysets

from itertools import chain
result_list = list(chain(q1, q2, q*))

The chain method returns a chained object, so you transform it into a list and then you can get the information.

You can also use the union method (Django Union Method)

qs1.union(qs2)

See more information in How can I combine two or more querysets in a Django view?

Bruno Urbano
  • 377
  • 2
  • 13
0

In your clientInventory method, you could add od_inven to your context dict.

    context['id_inven'] = id_inven
    context['od_inven'] = od_inven

Then you could use that in your template in the same way you are currently using id_inven. Something like:

...
<body>
{% for foo in od_inven %}
   ...
    <td>{{ foo.id }}</td>
...
Dharman
  • 30,962
  • 25
  • 85
  • 135
cavalcantelucas
  • 1,362
  • 3
  • 12
  • 34