title: How to do database migration using python script?
body: <p>is it possible to do migration from python script?</p>
  
  <p>I am trying to use django on Heliohost where there is no shell but I can use python script.</p>
  
  <pre><code>something like
  
  from django import shell
  shell.main(['mysite/manage.py', 'migrate'])
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>Using input from @Shadow, I tried putting this code in views.py file</p>
  
  <pre><code>def migrate(request):
      django.setup()
      from django.core.management import call_command
      call_command("migrate", interactive=False)
      return HttpResponse("Final Migration Successful")
  </code></pre>
  
  <p>Then visited the url mapped to migrate which returned "Final Migration Successful"</p>
  
  <p>then I tried the database like</p>
  
  <pre><code>from .models import Question
  
  
  class Question(models.Model):
      question_text = models.CharField(max_length=200)
      pub_date = models.DateTimeField('date published')
  </code></pre>
  
  <p>Gives me error:</p>
  
  <pre><code>(1146, "Table 'usr_mydb.polls_question' doesn't exist")
  </code></pre>
  
  <p><strong><em>From Django Tutorial:</em></strong></p>
  
  <p>Bypassing manage.py</p>
  
  <p>If you’d rather not use manage.py, no problem. Just set the DJANGO_SETTINGS_MODULE environment variable to mysite.settings, start a plain Python shell, and set up Django:</p>
  
  <pre><code>&gt;&gt;&gt; import django
  &gt;&gt;&gt; django.setup()
  </code></pre>
  
  <p>If this raises an AttributeError, you’re probably using a version of Django that doesn’t match this tutorial version. You’ll want to either switch to the older tutorial or the newer Django version.</p>
  
  <p>You must run python from the same directory manage.py is in, or ensure that directory is on the Python path, so that import mysite works.</p>
  
  <p>For more information on all of this, see the django-admin documentation.</p>
  
  <p>But how to do <code>python manage.py &lt;command&gt;</code> using the <code>django.setup()</code> is nowhere in the documentation.</p>