'2 models for html in Django

I want to list some data into my html file, below is my 2 models files and My question is how to show products_name in my html ?

class ProductsDescription(models.Model):
    products_id = models.ForeignKey(to='Products', to_field='products_id', on_delete=models.CASCADE, unique=True)
    products_name = models.CharField(max_length=255, blank=True, null=True) 

  class Products(models.Model):
    products_id = models.AutoField(primary_key=True)
    products_type = models.IntegerField(blank=True, null=True)
    manufacturers_id = models.IntegerField(blank=True, null=True)
    brand_id = models.IntegerField(blank=True, null=True)

products.py

from myadmin.models import Products as products
from myadmin.models import ProductsDescription

def index(request, pIndex=1):
    mainproducts = products.objects
    umproducts = mainproducts.filter(products_type=1)
    pIndex = int(pIndex)
    page = Paginator(umproducts, 25)
    maxpages = page.num_pages
    if pIndex > maxpages:
        pIndex = maxpages
    if pIndex < 1:
        pIndex = 1
    list2 = page.page(pIndex)
    plist = page.page_range
    context = {"umproducts": list2, 'plist': plist, 'pIndex': pIndex, 'maxpages': maxpages }
    return render(request, "index.html", context)

in my html

<table class="table table-hover">
      <tr>
      <th>ID</th> 
      <th>TYPE</th>
      <th>Products Name</th>
      <th>MANUFACTORES</th>
      <th>BRAND</th>
      </tr>
      {% for vo in umproducts %}
           <tr>
           <td>{{ vo.products_id}}</td>
           <td>{{ vo.products_type }}</td>
           <td>{{ vo.products_name }}</td>
           <td>{{vo.products_manufacturers}}</td>
           <td>{{ vo.products_brand}}</td>
           </tr>

The problem maybe is I cann't put the product_name from ProductsDescription into umproducts tuple,and my question is how to connect 2 models and namea new tuple then I can use it in html?



Solution 1:[1]

There's a few problems with this line...

products_id = models.ForeignKey(to='Products', to_field='products_id', on_delete=models.CASCADE, unique=True)

First change the name to product. You can still refer to ProductDescription.product_id and django will know you mean the id, otherwise you will double up on fields with the same name which will be a headache later. You probably don't need a products_id in your Products model either. Django creates ID fields automatically for models.

Next, give it a related name, eg related_name="descriptions" , for easier access.

Finally, you have specificied a foreign key with unique=True. A foriegn key with unique = True is basically a one-to-one relationship and django suggests create the tables that you use that instead.

So, in summary, you can use in ProductsDescription

 product = models.OneToOneField(Products, on_delete=models.CASCADE, related_name="description").  

NB: You will need to makemigrations then migrate again, as this is a db change.

The advantage is you can then refer to it in your template as

{{vo.product.description.product_name}}

Normally this would be a bit trickier, as a foreign key is usually many to one, so you would get a set and have to loop through it to find the right one. In this case, as you have unique =True, you might as well make use of one-to-one and save yourself some trouble.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SamSparx