简体   繁体   English

Django的信号与触发器?

[英]django-signals vs triggers?

I read about django signals ( http://docs.djangoproject.com/en/dev/topics/signals/ ), but as far as I understand, signals are never converted into literal SQL triggers ( http://en.wikipedia.org/wiki/Database_trigger ). 我了解了django信号( http://docs.djangoproject.com/en/dev/topics/signals/ ),但据我了解,信号从未转换为文字SQL触发器( http://en.wikipedia。 org / wiki / Database_trigger )。

If I'm correct that signals and triggers are different, then which one is better and in what ways? 如果我正确地认为信号和触发信号是不同的,那么哪个更好,在哪些方面呢? What's the best practice? 最佳做法是什么?

.................... ....................

Here's a concrete example if you want one: 这是一个具体的示例,如果您想要一个:

class Location(models.Model):
    name = models.CharField(max_length=30)

class Person(models.Model):
    location = models.ForeignKey('Location')

class Team(models.Model):
    locations = models.ManyToManyField('Location')

I want a person to be able to join a team if and only if that person's location is within that team's set of locations. 我希望一个人能够加入团队,前提是该人的位置在该团队的位置范围内。 I do not know how to do that with normal relational constraints, so as far as I know I'm forced to use triggers or signals. 我不知道如何使用正常的关系约束来做到这一点,据我所知,我被迫使用触发器或信号。 My gut says that I should use triggers but I want to know best practice. 我的直觉说我应该使用触发器,但是我想知道最佳实践。

Neither. 都不行 The best tool for this job is model validation - you can write your custom validation rule there and it will be enforced in the admin and your own apps. 这项工作的最佳工具是模型验证 -您可以在此处编写自定义验证规则,该规则将在管理员和您自己的应用中强制执行。

Django signals are awesome (validation is awesome too, but sometimes you need to change something before save…). Django信号很棒(验证也很棒,但是有时您需要在保存之前进行一些更改 ……)。 If you are working with database ONLY thru Django, it's really good idea to keep all logic in same place, imho. 如果您仅通过Django使用数据库,那么将所有逻辑都放在同一位置是个好主意,恕我直言。

Here is an example, how it works: 这是一个示例,其工作方式:

class Example(models.Model):
    ''' Example of Model (I hate foo-bars!) '''
    age = models.IntegerField()
    can_buy_beer = models.BooleanField(default=False)


def set_can_buy_beer(sender, instance, **kwargs):
    ''' Trigger body '''
    if instance.age >= 21:
        instance.can_buy_beer = True
    else:
        instance.can_buy_beer = False

# ↓ Magic — now, field Example.can_buy_beer will be autocalculated on each save!
pre_save.connect(set_can_buy_beer, sender=Example) 

You can use triggers to enforce this kind of constraints, but I wouldn't rely on that. 您可以使用触发器来强制执行此类约束,但是我不会依赖于此。 This can only be done as a secondary enforcement, while the primary one is to be model validation, just as Daniel already said. 正如Daniel所说,这只能作为第二种强制执行,而主要的是模型验证。

As for DB triggers vs Django signals they are more different the common. 至于DB 触发器与Django 信号,它们的共同点是更多不同。 The only common thing they share is that both are invoked upon entity change. 它们共享的唯一共同之处是两者都在实体更改时被调用。 But the entities differ very much. 但是实体之间有很大的不同。

Triggers monitor database row changes, thus they operate on raw tabular data. 触发器监视数据库行的更改,因此它们对原始表格数据进行操作。 Trigger code is run by DBMS. 触发代码由DBMS运行。

In contrast to triggers signals monitor domain object changes. 与触发信号相反,它监视域对象的变化。 In a generic case Django's model consists of data from several table rows (consider model inheritance and related object subsets). 在一般情况下,Django的模型由来自多个表行的数据组成(考虑模型继承和相关的对象子集)。 Signal code is run by Django. 信号代码由Django运行。

Main advantages of Triggers over Signals: 触发器优于信号的主要优点:

  • independent of the application: makes the migration to a new frameworks/languages easier (since triggers and, in some cases, stored procedure are dump with your DB) 独立于应用程序:简化了向新框架/语言的迁移(因为触发器和存储过程在某些情况下随数据库一起转储)

  • safety : depending on the situation, you could restrict the UPDATE rights on some tables and still be able to run your app (think of critical history or transaction tables, who knows which exploits might be discovered in the next 10 years) 安全性 :根据具体情况,您可以限制某些表的UPDATE权限,并且仍然可以运行您的应用程序(想想关键历史记录或交易表,谁知道未来10年内可能会发现哪些漏洞利用)

  • reduce the number of requests your app have to address to the DBMS (especially useful in the context of a distributed architecture). 减少您的应用程序必须向DBMS处理的请求数量(在分布式体系结构的上下文中尤其有用)。

Here are the main advantages. 这是主要优点。 The main cons is that you have to deal with the old school SQL syntax. 主要缺点是您必须处理旧式的SQL语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM