Search code examples
pythondjangomany-to-manyboolean-logic

Boolean combination of attributes in Django


I'm using Django to outline the relationship between university courses and high school subject requirements.

So far I have the following models:

class Course(models.Model):
   title = models.CharField(max_length=100)    #(e.g. Bachelor of _____)
   ft_years = models.SmallPositiveIntegerField()    #years of full time study

class Requirement(models.Model):    
   subject = models.CharField(max_length=50)    #(e.g. Mathematics 3)
   score = models.DecimalField(max_digits=3)    #(e.g. 70)       
   percent = models.NullBooleanField()    #True if score is in percent

My question is, how can I add a Boolean combination of Requirement to each Course?

For example:

A Bachelor of Physics (Course) has the following Requirements:
(70% in Physics) AND (50% in Mathematics 3 OR 45% in Mathematics 4)


Additional info: Because several courses may share the same requirement (e.g. 70% in Physics) but each course may have several requirements, I am planning to use a m2m model.


Solution

  • You could make a CompoundRequirement which has some number of requirements that it ORs together (with a ManyToManyField) and then each class has some Requirements and some CompoundRequirements. ANDs are handled by having more than one requirement, so all you need to handle is ORs.