In django, how do I call the subcommand 'syncdb' from the initialization script?

流过昼夜 提交于 2020-01-09 08:39:12

问题


I'm new to python and django, and when following the Django Book I learned about the command 'python manage.py syncdb' which generated database tables for me. In development environment I use sqlite in memory database, so it is automatically erased everytime I restart the server. So how do I script this 'syncdb' command?(Should that be done inside the 'settings.py' file?)

CLARIFICATION

The OP is using an in-memory database, which needs to be initialized at the start of any process working with Django models defined against that database. What is the best way to ensure that the database is initialized (once per process start). This would be for running tests, or running a server, either via manage.py runserver or via a webserver process (such as with WSGI or mod_python).


回答1:


All Django management commands can be accessed programmatically:

from django.core.management import call_command
call_command('syncdb', interactive=True)

Ideally you'd use a pre-init signal on runserver to activate this, but such a signal doesn't exist. So, actually, the way I'd handle this if I were you would be to create a custom management command, like runserver_newdb, and execute this inside it:

from django.core.management import call_command
call_command('syncdb', interactive=True)
call_command('runserver')

See the documentation for more information on writing custom management commands.




回答2:


As suggested by "Where to put Django startup code?", you can use middleware for your startup code. The Django docs are here.

For example (untested):

startup.py:

from django.core.exceptions import MiddlewareNotUsed
from django.conf import settings
from django.core.management import call_command

class StartupMiddleware(object):
    def __init__(self):
        # The following db settings name is django 1.2.  django < 1.2 will use settings.DATABASE_NAME
        if settings.DATABASES['default']['NAME'] == ':memory:':
            call_command('syncdb', interactive=False)

        raise MiddlewareNotUsed('Startup complete')

and in your settings.py:

 MIDDLEWARE_CLASSES = (
     'your_project.middleware.startup.StartupMiddleware',

     # Existing middleware classes here
 )



回答3:


Update

I added a script called run.sh in the project's root directory. This worked for me with an SQLite database:

#!/usr/bin/python
from django.core.management import call_command
call_command('syncdb')
call_command('runserver')

Original Answer

I am not sure I understand what you mean by "scripting the syncdb command". You usually execute python manage.py syncdb from the command line. This is usually done after adding new models. In case you want to you easily accomplish this using a simple shell script. I don't see any reason to place (or invoke) syncdb from within settings.py.

Could you add more details to your question? Add context and explain what exactly are you trying to get done?




回答4:


You could create a new script that you call instead of manage.py that calls manage.py:

from subprocess import call
call(["python", "manage.py", "syncdb"])
call(["python", "manage.py", "runserver"])

If you don't need to add an admin you could change the second line like this:

call(["python", "manage.py", "syncdb", "--noinput"])

I'm assuming that what you're trying to do is create your db and then start your server with one command every time.




回答5:


@Daniel Naab's answer, as well as the doc in the official site, is not for executing management commands as an entrypoint.

When you want to use a management command as the entrypoint in managed cloud environment like AWS Lambda or Google Cloud Functions, you can take a look at manage.py and try something similar.

import os
from django.core.management import execute_from_command_line

def publishing_fn(data, context):
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'YOURAPP.settings')
    # The first argument is "manage.py" when it's run from CLI.
    # It can be an empty string in this case
    execute_from_command_line(['', 'COMMAND', 'ARGS...'])


来源:https://stackoverflow.com/questions/3495964/in-django-how-do-i-call-the-subcommand-syncdb-from-the-initialization-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!