如何使用django将一个数据库一分为二
how to split one database into two with django
我有一个大数据 table(在 Postgresql 中),我想将其转换为两个 table
因此,我需要第一个 table 中大 table 中的某些列
Аnd 在第二个 table 我需要转移剩余的列
这是我的老model.py
class HV(models.Model):
from direction.models import Project
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=60)
address = models.CharField(max_length=90)
username = models.CharField(max_length=60)
password = models.BinaryField()
kind = models.CharField(max_length=60)
enabled = models.BooleanField(default=True)
availability = models.BooleanField(default=True)
time_unavailable = models.DateTimeField(null=True)
direction = models.ForeignKey(Department, on_delete=models.PROTECT, null=True, default=None)
project = models.ForeignKey(Project, on_delete=models.PROTECT, null=True, default=None)
class Meta:
managed = True
db_table = 'hv_list_service'
def __str__(self):
return self.name
和我的新 models.py
class ClusterName(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=60)
kind = models.CharField(null=True, max_length=10)
enabled = models.BooleanField(default=True)
availability = models.BooleanField(default=True)
time_availability = models.DateTimeField()
direction_id = models.ForeignKey(Department, on_delete=models.PROTECT)
project_id = models.ForeignKey(Project, on_delete=models.PROTECT)
class Meta:
managed = True
db_table = 'vmlist_cluster_name'
def __str__(self):
return self.name
class ClusterPooling(models.Model):
id = models.AutoField(primary_key=True)
address = models.CharField(null=True, max_length=120)
username = models.CharField(null=True, max_length=50)
password = models.BinaryField(null=True)
cluster_name_id = models.ForeignKey(ClusterName, null=True, on_delete=models.PROTECT)
active = models.BooleanField(null=True)
class Meta:
managed = True
db_table = 'vmlist_cluster_polling'
def __str__(self):
return self.address
我知道您可以使用 Django 将数据从一个 table 传输到另一个,但我不知道脚本应该是什么样子
我觉得你可以试试用django导入导出包https://django-import-export.readthedocs.io/en/latest/
完成此操作的最简单方法是在 psql 中执行这些命令
INSERT INTO vmlist_cluster_name (id,name,kind,enabled,availability,time_availability,direction_id_id,project_id_id)
SELECT id, name, kind,enabled ,availability ,time_unavailable ,direction_id ,project_id FROM public.hv_list_service
INSERT INTO vmlist_cluster_polling (id,address,username,password)
SELECT id, address, username, password FROM public.hv_list_service
我有一个大数据 table(在 Postgresql 中),我想将其转换为两个 table 因此,我需要第一个 table 中大 table 中的某些列 Аnd 在第二个 table 我需要转移剩余的列
这是我的老model.py
class HV(models.Model):
from direction.models import Project
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=60)
address = models.CharField(max_length=90)
username = models.CharField(max_length=60)
password = models.BinaryField()
kind = models.CharField(max_length=60)
enabled = models.BooleanField(default=True)
availability = models.BooleanField(default=True)
time_unavailable = models.DateTimeField(null=True)
direction = models.ForeignKey(Department, on_delete=models.PROTECT, null=True, default=None)
project = models.ForeignKey(Project, on_delete=models.PROTECT, null=True, default=None)
class Meta:
managed = True
db_table = 'hv_list_service'
def __str__(self):
return self.name
和我的新 models.py
class ClusterName(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=60)
kind = models.CharField(null=True, max_length=10)
enabled = models.BooleanField(default=True)
availability = models.BooleanField(default=True)
time_availability = models.DateTimeField()
direction_id = models.ForeignKey(Department, on_delete=models.PROTECT)
project_id = models.ForeignKey(Project, on_delete=models.PROTECT)
class Meta:
managed = True
db_table = 'vmlist_cluster_name'
def __str__(self):
return self.name
class ClusterPooling(models.Model):
id = models.AutoField(primary_key=True)
address = models.CharField(null=True, max_length=120)
username = models.CharField(null=True, max_length=50)
password = models.BinaryField(null=True)
cluster_name_id = models.ForeignKey(ClusterName, null=True, on_delete=models.PROTECT)
active = models.BooleanField(null=True)
class Meta:
managed = True
db_table = 'vmlist_cluster_polling'
def __str__(self):
return self.address
我知道您可以使用 Django 将数据从一个 table 传输到另一个,但我不知道脚本应该是什么样子
我觉得你可以试试用django导入导出包https://django-import-export.readthedocs.io/en/latest/
完成此操作的最简单方法是在 psql 中执行这些命令
INSERT INTO vmlist_cluster_name (id,name,kind,enabled,availability,time_availability,direction_id_id,project_id_id)
SELECT id, name, kind,enabled ,availability ,time_unavailable ,direction_id ,project_id FROM public.hv_list_service
INSERT INTO vmlist_cluster_polling (id,address,username,password)
SELECT id, address, username, password FROM public.hv_list_service