Home > front end >  how to split one database into two with django
how to split one database into two with django

Time:05-19

I have one big data table (in Postgresql) that I want to turn into two tables Accordingly, I need certain columns from the big table in the first table Аnd in the second table I need to transfer the remaining columns

here my old 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

and my new 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

I know you can transfer data from one table to another using Django, but I have no idea what the script should look like

CodePudding user response:

I think you can try to use django import export package https://django-import-export.readthedocs.io/en/latest/

CodePudding user response:

the easiest way to get this done is by doing these commands in 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
  • Related