【问题标题】:Django rest-framework with durable_rules带有durable_rules的Django rest-framework
【发布时间】:2020-06-29 05:14:40
【问题描述】:

我正在尝试将耐用规则与我的 django restframework api 集成

我需要一些关于如何进行此操作的指导

比方说,我有一个班级学校、班级学生、班级地点

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()

现在,我想制定规则说

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

如何使用耐用规则执行此操作?我的问题是我在哪里为它编写代码,在视图或序列化程序中。

任何示例代码都会有很大帮助

谢谢

现在以学校为准

【问题讨论】:

    标签: python-3.x django-rest-framework rule-engine durable-rules


    【解决方案1】:
    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)
    

    ##你也可以使用 django 信号 post save 和 pre save.

    【讨论】:

    • 感谢 Madmax。我提到的只是一组示例规则。我会有很多规则,并想为此使用耐用规则引擎。如何将耐用规则与我的所有类集成?
    • 你知道怎么做吗?
    猜你喜欢
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 2014-06-01
    • 2015-06-28
    • 2014-04-07
    • 2019-04-26
    • 2013-04-12
    • 1970-01-01
    相关资源
    最近更新 更多