0

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!

loar
  • 1,505
  • 1
  • 15
  • 34

1 Answers1

0

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.

cezar
  • 11,616
  • 6
  • 48
  • 84
  • Hi, @cezar, thanks for your answer, I'll try to be more specific. I have a contract which can be extended to another contract which, in turn, can result to another one. Each extended contract will be extended from its previous contract not from the first one. For example, a contract comes to an end but is extended so it should be related with its previous one. Besides, each contract should always be related with a person (same person in every extended contract). So, I think the best choice, according to your options, would be use a ForeignKeyField('self'). Thanks for you help. – loar Nov 18 '14 at 09:21
  • 1
    In that case the best option would be OneToOneField('self'). As you say a contract can be extended to another contract, but not to another contract**S**. The second contract can be extended even further to a third contract, but the third contract is not related to the first. That means one contract can have one extended contract and in turn an extended contract can have only one predecessor. Clear case of 1:1 relation. – cezar Nov 18 '14 at 09:28