Home > Enterprise >  Fetching foreign key's tables data from primary key's table and to render to templete in d
Fetching foreign key's tables data from primary key's table and to render to templete in d

Time:12-30

These are my models:

class Customer2(models.Model):
    Customer_Name=models.CharField(max_length=30)
    Customer_Address=models.CharField(max_length=100)
    Customer_Phone=models.IntegerField()
    Customer_Email=models.EmailField(max_length=50)
    class Meta:
    db_table="Customer_Table"

class Product(models.Model):
    Product_Name=models.CharField(max_length=100)
    Quantity=models.FloatField(max_length=100)
    Comming_Date=models.DateField(max_length=15)
    Expire_Date=models.DateField(max_length=15)
    Comming_Price=models.FloatField(max_length=50)
    Picture=models.ImageField(upload_to='Images',blank=True, null=True)

class Sale(models.Model):
    Customer=models.ForeignKey(Customer2, default=1, on_delete=models.CASCADE)
    Product=models.ForeignKey(Product, default=1, on_delete=models.CASCADE)
    Quantity=models.FloatField()
    Price=models.FloatField()
    Date=models.DateTimeField()
    Customer_Name1=models.CharField(max_length=20,default=0)
    Product_Name1=models.CharField(max_length=20,default=0)
    class Meta:
    db_table="Sale_Table"

I want to get or fetch or show all Sale model( class Sale) data and render to templete but instead of Customer and Product columns in Sale table(model),I want to show Customer_Name and Product_Name from their own tables what should I do?

CodePudding user response:

You can fetch related fields like this:

 sales = Sale.objects.all().values('Quantity', 'Customer__Customer_Name', 'Product__Product_Name')
 # you can include all other fields as comma separated values in values()
 for sale in sales:
       print(sale)

I would suggest keeping column names in all lower case as that is the convention like instead of Customer_Name just customer_name similarly with other fields.

CodePudding user response:

To access the foreign keys fields, and the fields on those models, you use dot notation:

for sale in Sale.objects.all():
    print(sale.Customer.Customer_Name, sale.Product.Product_Name)

To render the data in a template, you need to first create a view, and pass data to your template via context:

from django.http import HttpResponse
from django.template import loader

from .models import Sale


def sales_view(request):
    sales = Sale.objects.all()
    template = loader.get_template('your_app_name/sales.html')
    context = {
        'sales': sales,
    }
    return HttpResponse(template.render(context, request))

Then in your template, you can access the foreign keys directly:

{% if sales %}
    <ul>
    {% for sale in sales %}
        <li>Customer: {{ sale.Customer.Customer_Name }}</li>
        <li>Product: {{ sale.Product.Product_Name }}</li>
    {% endfor %}
    </ul>
{% else %}
    <p>No sales are available.</p>
{% endif %}

See the official django tutorial for more information.

  • Related