2

I am trying to integrate durable_rules with my django restframework apis

I need some guidance as to how to proceed with this

Lets say, I have a class school , class student, class location

class School(models.Model):
schoolname = models.CharField(max_length=200)

class Location(models.Model):
locationname = models.CharField(max_length=200)

class Student(models.Model):
school = models.ForeignKey(School)
location = models.ForeignKey(Location)
studentname = models.CharField(max_length=200)
fees = models.IntegerField()

Now, I want to make rules saying

if (schoolname = 'ABC' and location = 'xyz') then update fees = 1000
if (schoolname = 'SDS' and location = 'sdfs') then update fees = 200

How do I do this with durable_rules? My question if more on where do I write the code for it, in views or serializers.

Any sample code would be of great help

Thanks

Now based on the school

1 Answers1

0
class Student(models.Model):
  school = models.ForeignKey(School)
  location = models.ForeignKey(Location)
  studentname = models.CharField(max_length=200)
  fees = models.IntegerField()

  #You can overide the Student Model save method 
  def save(self, *args, **kwargs):
    if self.schoolname == 'ABC' and self.location = 'xyz':
        self.fees = 1000
    if self.schoolname = 'SDS' and self.location = 'sdfs':
        self.fees = 200
    super(Student, self).save(*args, **kwargs)

##you can also use django signals post save and pre save.

Madmax
  • 125
  • 7
  • Thanks Madmax. What I have mentioned is only a sample set of rules. I will have a lot of rules and would like to use durable_rules engine for it. How do I integrate durable_rules with all of my classes? – rathimittha Jun 29 '20 at 12:25
  • Were you able to figure out how to do this? – bir singh Feb 24 '22 at 11:39