简体   繁体   English

复制 Django Model 输入到另一个 Django 数据库

[英]Copy Django Model Input into another Django Database

I am trying to figure out if there was a way that once I uploaded something to one of my Django database files, that, that same upload would also upload to another database file, so if I go to delete one of them, it is saved in the other.我试图弄清楚是否有一种方法,一旦我将某些内容上传到我的 Django 数据库文件之一,同样的上传也会上传到另一个数据库文件,所以如果我 go 删除其中一个,它会被保存在另一个。

class CompanyInfo(models.Model):
  company_name = models.CharField("Company Name", unique=True, max_length=50, default="")

# Identifiers
identifier = models.IntegerField(default=0)



# Company Information
sector = models.CharField("Sector", max_length=50,default="")
location = models.CharField("Location", max_length=100, default="")
year_founded = models.IntegerField("Year Founded", default=2000)
company_description = models.TextField("Company Description", default="")
state_registered_in = models.CharField("State of Registration",  choices=states_list, max_length=2, default=2)
company_url = models.URLField("Company URL", max_length=200, default="", blank=True, null=True)
phone_number = models.CharField("Phone Number", max_length=20,default="")
company_logo_url = models.ImageField("Company Logo", upload_to=None, height_field=None, width_field=None, max_length=100, blank=True, null=True)
facebook = models.URLField("Facebook URL", max_length=200, default="", blank=True, null=True)
linkedin = models.URLField("Linkedin URL", max_length=200, default="", blank=True, null=True)
instagram = models.URLField("Instagram URL", max_length=200, default="", blank=True, null=True)
email = models.EmailField("Email Address", max_length=40,default="")




def __str__(self):
    return "{} from {}".format(self.company_name, self.location)

Basically, once these values are created for the "CompanyInfo" class, I want it to also upload itself into another Django class in the exact same way, so if I eventually delete the input in the "CompanyInfo" class, there will still be a saved version in the other created database. Basically, once these values are created for the "CompanyInfo" class, I want it to also upload itself into another Django class in the exact same way, so if I eventually delete the input in the "CompanyInfo" class, there will still be a保存在另一个创建的数据库中的版本。

There's a couple of options like overriding save or using a model manager, but the easiest and more testable method is to use a post_save signal: https://docs.djangoproject.com/en/dev/ref/signals/#post-save有几个选项,例如覆盖 save 或使用 model 管理器,但最简单和更可测试的方法是使用 post_save 信号: https://docs.djangoproject.com/en/dev/ref/signals/#post-save

This allows you to grab model instances that are saved and updated, but do note that when an .update() call is made, the signal is skipped which may be undesirable.这允许您获取已保存和更新的 model 实例,但请注意,当进行.update()调用时,会跳过信号,这可能是不可取的。

The way you can save to your backup database is by writing the signal to extract all fields and resave.保存到备份数据库的方法是编写信号以提取所有字段并重新保存。 I would avoid the update_fields since some devs will not use it (it helps with optimizing queries).我会避免使用update_fields ,因为一些开发人员不会使用它(它有助于优化查询)。

def save_to_backup(sender, instance, created: bool, using: str, **kwargs):
    if using == "default":
        # You don't want to continuously save/i.e. infinite loop
        return
    fields = instance._meta.get_all_field_names()
    sender.objects.using("backup_alias").save(**{field: instance._meta.get_field(field) for field in fields})

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

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