I have a parent model defined as :
class PurchaseOrder(models.Model):
number = models.CharField(max_length=30, unique=True)
def services(self):
return self.service_set.filter(purchaseorder=self.pk)
Child model Service is defined as :
class Service(models.Model):
purchaseorder = models.ForeignKey(PurchaseOrder)
modules = models.ManyToManyField(Module)
def get_modules(self):
return self.modules.all()
There is another model Module having M2M relation with the Service model. I wish to display the modules as a list in the Parent model change_list view i.e in list_display list. How do I do that? What is the effect on the number of database hits?
Using your example, I do something like this
then in admin.py
I’m not sure about the db hits but you can use a tool like django-debug-toolbar to see the SQL queries generated before and after you implement it.
UPDATED BASED ON COMMENT
Try the above list comprehension and see if it works? You might need to play around with the output a bit to get it right in shell.