Is there a way to select some specific fields from model with Foreign Key. Here is an example: let's say I have
class Product(models.Model):
CATEGORY = (
('A', 'A'),
('B', 'B'),
('C', 'C'),
)
name = models.CharField(max_length=200, null=True)
price = models.FloatField(null=True)
category = models.CharField(max_length=200, null=True, choices=CATEGORY)
description = models.CharField(max_length=200, null=True)
date_created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
class Order(models.Model):
STATUS = (
('Work Under Progress', 'Work Under Progress'),
('Delivered', 'Delivered'),
)
product = models.ForeignKey(Product, null=True, on_delete=models.SET_NULL)
date_created = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=200, null=True,
choices=STATUS)
def __str__(self):
return self.product.name
The purpose is to get the product name and product price in the Order class. Is there any way to do so? (I'm very new to Django and could find exactly this in the doc)
Thanks