0

hey guys i have these models

class Product(models.Model):
     .......
class Course(Product): 
     .........
class Book(Product):
     .........
class Cart(models.Model):
    product = models.ForeignKey(Product)

what I want is to prefetch the products with the Cart objects i know we can do this

Cart.objects.select_related('product')

but how do we also get the product to children too without making an impact on the performance and if i get it's children how can get it's child when accessing the product like:

cart_instance.product.its_child
th3plus
  • 161
  • 2
  • 11

1 Answers1

0
Cart.objects.select_related('product').select_related('product__its_child')

Refer this: https://docs.djangoproject.com/en/4.0/ref/models/querysets/#select-related

Msvstl
  • 1,116
  • 5
  • 21
  • but how can i access it's corresponding child since i have course and book @msvstl – th3plus Aug 02 '22 at 11:35
  • @th3plus did you read this? [should-i-avoid-multi-table-concrete-inheritance-in-django-by-any-means](https://stackoverflow.com/questions/23466577/should-i-avoid-multi-table-concrete-inheritance-in-django-by-any-means) – Msvstl Aug 02 '22 at 11:47
  • Check whether this solves your problem. https://stackoverflow.com/a/53341978/13779320 – Msvstl Aug 02 '22 at 11:49
  • yeah i did read this before, is it better to use content_type and generic relationship instead of multitable inheritance? @msvstl – th3plus Aug 02 '22 at 12:08
  • @th3plus It's based on your requirement, check [what-are-the-pros-and-cons-of-using-genericforeignkey-vs-multitable-inheritance](https://stackoverflow.com/questions/30562039/what-are-the-pros-and-cons-of-using-genericforeignkey-vs-multitable-inheritance) – Msvstl Aug 02 '22 at 12:45