Django M2M self to relate an extend version of a table

65 views Asked by At

I have two tables: Project and Contract. One project result in a contract. Plus, a contract could be extended leading to another contract related to the previous one. So I think I could use something like: contract = models.ManyToManyField('self')

Any idea?

Thanks!

1

There are 1 answers

2
cezar On BEST ANSWER

It depends on your requirements. A contract can result in another contract or another contractS? That is very important questions. If we assume that a contract A can lead to another contract B (but not to other contracts) and that the new contract B can be extended only from contract A, then OneToOneField('self') would be appropriate.

If a contract A can lead to contract B, but also to contract C or maybe contract D, then the ForegnKey('self') should be used. But if contract B can be traced back not only to contract A, but also to contract E, or maybe even contract F and contract G, then you need ManyToManyField('self').

So it is very basic question first to clarify if it as a 1:1, 1:n or m:n relation. After figuring this out you'll have your right answer.

From the information you provided I can just vaguely guess what should be the right approach.

I hope my answer can help you.