Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否触发将添加到数据库的新行发送到postgres中的另一个数据库?_Python_Django_Postgresql - Fatal编程技术网

Python 是否触发将添加到数据库的新行发送到postgres中的另一个数据库?

Python 是否触发将添加到数据库的新行发送到postgres中的另一个数据库?,python,django,postgresql,Python,Django,Postgresql,我在两台不同的服务器上有两个数据库(例如A和B)。 我有一些爬虫程序使用API函数将数据写入数据库(A)。其中一些数据是新的,而另一些只是对前几行的更新。如果在(A)中添加了一些新行,我想在(B)中添加这些新行的id。 我希望这种情况能够实时发生,而不需要(A)的完全扫描。我想要(a)中的触发器之类的东西来向(B)发送新ID。我应该如何编写此触发器?(如果您认为有更好的解决方案,请帮助我)您可以使用django信号来完成。信号的意思和触发器一样。如果要在创建、更新和删除模型时执行任何操作。你可

我在两台不同的服务器上有两个数据库(例如A和B)。
我有一些爬虫程序使用API函数将数据写入数据库(A)。其中一些数据是新的,而另一些只是对前几行的更新。如果在(A)中添加了一些新行,我想在(B)中添加这些新行的id。



我希望这种情况能够实时发生,而不需要(A)的完全扫描。我想要(a)中的触发器之类的东西来向(B)发送新ID。我应该如何编写此触发器?(如果您认为有更好的解决方案,请帮助我)

您可以使用django信号来完成。信号的意思和触发器一样。如果要在创建、更新和删除模型时执行任何操作。你可以使用信号。

假设您有两个名为的模型:

class A(models.Model):
  name = models.CharField(max_length=128)

class B(models.Model):
  a = models.ForeignKey(A)
将此文件保存在你的app/signals.py中

from django.db.models.signals import post_save
from .models import A,B

def create_b(sender, instance, created, *args, **kwargs):

    if created:
       B.objects.create(a=instance)

post_save.connect(create_b, sender=A)
为了正常工作,你必须在apps配置中调用它。 然后在
app.py
文件中复制此

class ProductConfig(AppConfig): #this will be different
    name = 'product' # this will be different based on your app name
#copy below lines only
    def ready(self):
        from .signals import *
在应用程序的
\uuuu init\uuuu.py
文件中包括这一行

default_app_config = "product.apps.ProductConfig"

您可以通过使用django信号来实现。信号的意思和触发器一样。如果要在创建、更新和删除模型时执行任何操作。你可以使用信号。

假设您有两个名为的模型:

class A(models.Model):
  name = models.CharField(max_length=128)

class B(models.Model):
  a = models.ForeignKey(A)
将此文件保存在你的app/signals.py中

from django.db.models.signals import post_save
from .models import A,B

def create_b(sender, instance, created, *args, **kwargs):

    if created:
       B.objects.create(a=instance)

post_save.connect(create_b, sender=A)
为了正常工作,你必须在apps配置中调用它。 然后在
app.py
文件中复制此

class ProductConfig(AppConfig): #this will be different
    name = 'product' # this will be different based on your app name
#copy below lines only
    def ready(self):
        from .signals import *
在应用程序的
\uuuu init\uuuu.py
文件中包括这一行

default_app_config = "product.apps.ProductConfig"

您希望通过django实现??如果可以使用django,是的。您希望通过django实现??如果可以使用django,是的。是否可以在不使用django的情况下将信号包含在数据库中?为此,我认为您需要使用触发器。在sql中,应该有类似django信号的触发器。但是我不知道怎么做。您只需搜索它。正如您所说,它在postgresql中也可以使用触发器和http请求。是否可以在不使用django的情况下将信号包含在数据库中?为此,我认为您需要使用触发器。在sql中,应该有类似django信号的触发器。但是我不知道怎么做。您只需搜索它。正如您所说,它在postgresql中也可以使用触发器和http请求。