'Structure of models to store a subset of choices to be available dependent on row being edited

I'm building a site that tracks parts, these part belong to a subsection of project, denoted by section codes. I want to be able to limit the choices of codes based on which project the part belongs to, but not sure the best way to store a list of acceptable codes.

I started with:

class Project(models.Model):
  name = models.CharField(max_length=40)
  number = models.IntegerField()

class Section(models.Model):
  code = CharField(max_length=1)

class Part(models.Model):
  number = models.IntegerField()
  project = models.ForeignKey(Project, on_delete=models.PROTECT)
  section = models.ForeignKey(Section, on_delete=models.PROTECT)  

The problem with this is that all parts then get to choose from the same list of section codes.

--- edit1 ---

Thinking about it a bit more, is this a many-to-many? A given project has many codes, and a given code can apply to many projects.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source