I'm making a django application and I'm facing an issue. I am trying to define a model where one ForeignKey
would be depending on another ForeignKey
.
Description part
My application is about making choices.
So let's say you have a decision
to make, a decision
has multiple choice
s, and a choice
has a status
(because of other constraints).
A status
can be used on multiple choices
, but a status
can be relevant only for one decision
, the same one the choice
is linked to.
Database schema
It isn't fixed and might be changed if needed :
,____________, ,____________,
| | 1, n 1, 1 | |
| Decision |------------------| Status |
|____________| |____________|
| |
| 1, n | 1, n
| |
| 1, 1 |
,_____|______, |
| | 1, 1 |
| Choice |-------------------------'
|____________|
Code
And here is my current (simplified) (not working) code :
class Decision (models.Model):
name = models.CharField (max_length = 63)
class Status (models.Model):
value = models.CharField (max_length = 63)
decision = models.ForeignKey (Decision)
class Choice (models.Model):
name = models.CharField (max_length = 63)
decision = models.ForeignKey (Decision)
status = models.ForeignKey (Status, limit_choices_to = {'decision' : decision})
The important part here being the limit_choices_to = {'decision' : decision}
.
Extra info
I found another SO question (In django, how to limit choices of a foreignfield based on another field in the same model?) dealing about the same question, but the question is becoming old, and the best answer was relying on an external app (django-smart-selects).
I'd rather not have to use something external, and I cannot see why something as simple as a 3-table relationship cannot be solved using only Django !
If someone has any solution, or any suggestion, please tell me.