If I have the following model:
class Contact(models.Model)
name = models.CharField(max_length=100)
...
class ContactAddress(models.Model)
line1 = models.CharField(max_length=100)
line2 = models.CharField(max_length=100)
...
contact = models.ForeignKey(Contact)
I now want to grab all Contacts and for the address to be auto populated. What would be the best way to do this? The only way I have found so far is to filter out the Contacts I want and loop around each contact and assign this to Contact.addresses. I then use this for outputting each Contacts address within a template.
Is there a better way of doing this? Select_related() almost does what I want, but doesn't seem to be able to work in the opposite direction.
Thanks in advance for your help on this one!
You are right, select_related
only works forwards! To make more efficient reverse lookups see this!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With