I have a model where tasks are pieces of work that each may depend on some number of other tasks to complete before it can start. Tasks are grouped into jobs, and I want to disallow dependencies between jobs. This is the relevant subset of my model:
class Job(models.Model):
name = models.CharField(max_length=60, unique=True)
class Task(models.Model):
job = models.ForeignKey(Job)
prerequisites = models.ManyToManyField(
'self',
symmetrical=False,
related_name="dependents",
blank=True)
Is there any way I can express the constraint that all prerequisite tasks must have the same job? I could enforce this at the view level, but I would really like to get it to work at the model level so that the admin interface will display appropriate options when choosing prerequisites for a task. I thought I could use "limit_choices_to", but on closer inspection it seems to require a static query, not something dependent on the values in this task object.