[Fixed]-Django: How to migrate dynamic models made at runtime

23👍

A simple solution worked for me. I ended up running the makemigrations and migrate management commands like so, after creating the dynamic model:

from django.core.management import call_command
call_command('makemigrations')
call_command('migrate')

3👍

Subprocess can migrate your model using migrate command. So try this it will work

import subprocess
command = 'python manage.py migrate'
proc = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = proc.communicate(command)

Read also this https://code.djangoproject.com/wiki/DynamicModels If it can help for create dynamic model

Leave a comment