Hi all -
I am interested in a function collecting the ‘child’ objects of a an object automatically. For example, from a Question, collect the Answers to it, and any ‘Comments’ on the Question but using the same code, collect the Questions that have on a particular Poll. Things connected by a OneToOne, ForeignKey or ManyToMany.
Using Model._meta.get_fields() I can get a field that is a “Many-To-One Relationship” from the Question to the Answer model. Naively, I would like to do (and have been trying) something like:
for obj in objs:
relation_fields = [f for f in obj._meta.get_fields() if field.is_relation and field.concrete]
child_objs[obj] = []
for field in relation_fields:
co = getattr(obj, field.name).all()
child_objects.append(co)
# alternative way - this works if a related_name is specified for the reverse relation, but
# the same type raises an "ManyToOneRel error not callable" for a different field that lacks
# 'related_name' but is of the same type
field(manager='objects).all()
I am looking for a consistent interface for objects that refer to my main object, and so far coming up blank. There are so many accessors and its such a thoughtful ORM, but I am stuck on this one. If I can’t find anything better, I’ll slog through by handling a lot of special cases explicitly. Have the feeling that if I understood something better, I’d be on my way though.
`