In our app we have several relationships and several models, I'm trying to achieve a generic way to get all related objects of an object, even reverse ones.
If I print ._meta.get_fields()
from my model Pessoa
, i get these relationship fields(I omitted the 'normal' ones):
<ManyToManyRel: cadastroimoveis.pessoa>
<ManyToOneRel: cadastroimoveis.pessoa_pessoa>
<ManyToOneRel: cadastroimoveis.pessoa_pessoa>
<ManyToOneRel: cadastroimoveis.pessoa_itr>
<ManyToManyRel: cadastroimoveis.doc>
<ManyToOneRel: cadastroimoveis.doc_pessoa>
cadastroimoveis.Pessoa.relacoes
cadastroimoveis.Pessoa.itrs
This specific model only has M2M relationships, and all of them contain a 'through' model as specified Here.
As you can see, it repeats them, one for the model, and one for the 'through' intermediate table(also a model I guess). And in the case of the recursive relationship it repeats twice.
My question is, is there a way to get these non repeated?
A way to know which repeated fields 'point' to the same relationship in the end(even though it spams two tables)? Because if the through table has fields, I want to display them in a different manner.
And according to the Model _meta API documentation, you would use this to get all related objects :
[
f for f in MyModel._meta.get_fields()
if (f.one_to_many or f.one_to_one)
and f.auto_created and not f.concrete
]
But the 'through' tables are not considered auto_created and are concrete.
Example :
<ManyToManyRel: cadastroimoveis.ccir>
<ManyToOneRel: cadastroimoveis.ccir_pessoa>
These two fields 'point' the same relationship, one is the intermediate table and the other is the model, is there a (automatic) way to know that these two are correlated? I couldn't find any attribute that they share.
The reason for this is because when the through table has fields, I need to edit on it instead of the M2M field on the model itself
Models.py : http://pastebin.com/szDfhHQ3 I cleaned the best I could