[Answer]-Django lookups into a nav table in M2M relationship

1👍

The problem is that the name and strength attributes belong to different objects (strength belongs to rel_med_subs, name to substance)! You can’t refer to both of them through the (s.name, s.strength) for s in self.rel_med_subs.order_by('order') comprehension – the s can only have name or strength.

It seems that the rel_med_subs attribute is a relation manager for objects of type substance and not of type rel_med_subs. Check the attributes of medication using dir to see which is the name of the manager for rel_med_subs (probably something like rel_med_subs_set — I really can’t remember it and always check it with dir. However be assured that medication will have two reverse relation attributes: One for the Many 2 Many relation with substance and one with the One 2 Many with rel_med_subs. After you find the correct reverse manager then you can use s.id_subs.name and s.strength.

Also please capitalize your class names and use corrent naming (rel_med_subs should be named MedicineSubstanceRelation) !

Leave a comment