Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access model's class level variable in data migration?

Here is my model.

Poll(models.Model):
   title = models.CharField(max_length=1024)
   MY_VAR = ['my_class_level_attribute'] # I want to access this

Here is my data migration:

def my_func(apps, schema_editor):
    Poll = apps.get_model('my_app', 'Poll')
    print Poll.MY_VAR


class Migration(migrations.Migration):

    dependencies = [
        ('webmerge', '0012_previous_migration'),
    ]

    operations = [
        migrations.RunPython(my_func)
    ]

The line print Poll.MY_VAR gives an attribute error. I think the issue might is in how get_model performs within a data migration because the following lines succeed in a Django shell:

In [2]: from django.apps import apps
In [3]: Poll = apps.get_model('my_app', 'Poll')
In [4]: Poll.MY_VAR
Out[4]:  ['my_class_level_attribute']
like image 297
davidhwang Avatar asked Aug 19 '15 18:08

davidhwang


People also ask

What is the lesson learned from the data migration process?

The lesson learned was that the test data must be random and should have sufficient breadth. I also believe that the data migration process should be executed as a Financial System where at every step the auditors can audit. What I mean here is several levels of reconciliation should happen as a part of the plan.

What are the different types of data migration?

A single data migration process can involve different types, including: 1. Storage Migration Storage migration is where a business migrates data from one storage location to another. It means moving data from one physical medium to another.

What is storage migration and how does it work?

The movement is not driven by a lack of space but rather a desire to upgrade storage technology. It normally does not alter the content or format of data. During storage migration, certain steps such as data validation, cloning, and data cleaning and redundancy can be carried out.

How should the data migration process be executed?

I also believe that the data migration process should be executed as a Financial System where at every step the auditors can audit. What I mean here is several levels of reconciliation should happen as a part of the plan. The first level of recon should be a simple sum of a numeric field, a number of rows, minimum/maximum of a date field etc.


1 Answers

You should be able to import the model

from my_app.models import Poll

If you do this, you shouldn't delete the Poll model or the MY_VAR attribute, otherwise your migrations will stop working.

like image 126
Alasdair Avatar answered Nov 02 '22 11:11

Alasdair