sql >> データベース >  >> RDS >> Mysql

Djangoフレームワークでレガシーデータベースを処理する方法

    レガシーモデルにいくつかのメソッドを追加して、関連するアイテムをもう少し効率的に取得できます。

    class Listflower(models.Model):
        especies_id = models.AutoField(primary_key=True)
        family_id = models.IntegerField(blank=True, null=True)
        especies = models.CharField(max_length=255, blank=True, null=True)
        
    
        class Meta:
            managed = False
            db_table = 'listflower'
    
        def get_family(self):
            return FamilyModel.objects.get(family_id=self.family_id)
    
    
    class Estate(models.Model):
        estate_id = models.AutoField(primary_key=True)
        estate_name = models.CharField(max_length=100, blank=True, null=True)
     
    
        class Meta:
            managed = False
            db_table = 'estates'
    
        def get_flowers(self):
            flower_ids = Flower2Estate.objects.filter(estate_id=self.estate_id).values_list('especies_id', flat=True)
            return Listflower.objects.filter(especies_id__in=flower_ids)
    

    ただし、このデータを操作するのが最後ではない場合は、fksを使用して通常のdjangoモデルを定義し、スクリプトを1回記述して、レガシーデータを新しいモデル構造に変換することをお勧めします。チャレンジには1時間以上かかることはありません。

    更新

    class FlowerFamily(models.Model):
        # assuming you old family model has 
        # "family_id" and "family_name" fields
        family_id = models.IntegerField(blank=True, null=True)
        family_name = models.CharField(max_length=255, blank=True, null=True)
    
    
    class Flower(models.Model):
        # you might want preserve old model fields in the new model
        # at least id fields
        especies_id = models.IntegerField(blank=True, null=True)
        family_id = models.IntegerField(blank=True, null=True)
        especies = models.CharField(max_length=255, blank=True, null=True)
    
        family = models.ForegnKey(FlowerFamily, related_name='flowers')
    
    
    class NewEstate(models.Model):
        estate_id = models.IntegerField(blank=True, null=True)
        estate_name = models.CharField(max_length=100, blank=True, null=True)
        flowers = models.ManyToManyField(Flower, related_name='estates')
    
    
    
    
    # this is a slightly primitive example
    # in real life you might want to use get_or_create instead of direct creation 
    # in case script fails and you'll need to run it again
    # also objects.get() might better be used with try-except ObjectDoesNotExist
    def convert_legacy():
        # create new instances
        for ff in YourOldFamilyModel.objects.all():
            new_ff = FlowerFamily(family_id=ff.family_id, family_name=ff.family_name)
            new_ff.save()
    
        for fl in Listflower.objects.all():
            new_fl = Flower(...)
    
            family = FlowerFamily.objects.get(family_id=fl.family_id)
    
            new_fl.family = family
            new_fl.save()
        
        # same thing for Estate
        for ...:
            new_estate ...
            new_estate.save()
    
            # and restore relations
            flower_ids = Flower2Estate.objects.filter(estate_id=new_estate.estate_id).values_list('especies_id', flat=True)
    
            for new_lf in Flower.objects.filter(especies_id__in=flower_ids):
                new_estate.flowers.add(new_fl)
            
        
        
    



    1. PostgreSQLで同等のDATEADD

    2. RailsからMySQLストアドプロシージャを呼び出す方法は?

    3. MYSQL-66キロバイトの行サイズ制限を回避する方法

    4. ORA-00997の回避策:LONGデータ型の不正使用