Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django migrations how to refer to an inner class inside model

I am looking to refer to a inner class 'Code' under the model 'Evaluation'. Basically both of these options DO NOT work (Option:1)

code = apps.get_model('my_project', 'Evaluation.Code') 

OR (Option:2)

evaluation = apps.get_model('my_project', 'Evaluation')
code = evaluation.Code

Option:1 throws me this error:

grzsqrrel@grzsqrreld:~/PycharmProjects/my_project/my_project$ python manage.py migrate
    local_settings.py not found
    Operations to perform:
    Apply all migrations: admin, auth, contenttypes, django_cron, sessions, my_project_app
    Running migrations:
    Applying my_project_app.0002_load_items...Traceback (most recent call last):
    File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
    File "/home/grzsqrrel/.virtualenvs/my_project/lib/python3.5/site-packages/django/core/management/__init__.py", line 363,...
    File "/home/grzsqrrel/.virtualenvs/my_project/lib/python3.5/site-packages/django/db/migrations/operations/special.py", line 193, in database_forwards
    self.code(from_state.apps, schema_editor)
    File "/home/grzsqrrel/PycharmProjects/my_project/my_project/my_project_app/migrations/0002_load_items.py", line 7, in load_data
    code = evaluation.Code
    AttributeError: type object 'Evaluation' has no attribute 'Code'

models.py:

class Evaluation(models.Model):
    class Code:

0002_load_items.py

def load_data(apps, schema_editor):
    evaluation = apps.get_model('my_project', 'Evaluation')
    code = evaluation.Code

class Migration(migrations.Migration):
    dependencies = [
        ('my_project', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(load_data)
    ]   

What'd be the fix? Thank you.

like image 254
Grizzled Squirrel Avatar asked Aug 31 '25 22:08

Grizzled Squirrel


1 Answers

turned out, the answer is elsewhere. Since I am not creating objects using the inner class, I can use conventional import.

from django.db import migrations
from my_project.models import Evaluation

def load_data(apps, schema_editor):
    evaluation = apps.get_model('my_project', 'Evaluation')
    code = Evaluation.Code
    my_model.objects.create(item_code=code.COURSES_TAUGHT)
like image 85
Grizzled Squirrel Avatar answered Sep 04 '25 05:09

Grizzled Squirrel