Override runserver django 1.7

648 views
Skip to first unread message

Carlos Perche

unread,
Oct 6, 2014, 10:47:50 AM10/6/14
to django...@googlegroups.com
Hello, why not is possible override the command runserver.py in my custom app in Django 1.7, like this

# encoding: utf-8
from django.conf import settings
from django.core.management.base import CommandError, BaseCommand
# from django.contrib.staticfiles.management.commands.runserver import Command as RunserverCommand
from django.core.management.commands.runserver import Command as RunserverCommand

class Command(RunserverCommand):
    
    def handle(self, addrport='', *args, **options):
        database = options.get('database', 'default')
        if (settings.DATABASES[database]['ENGINE'] == 'saas.backends.postgresql_psycopg2' or
                RunserverCommand is BaseCommand):
            raise CommandError("migrate has been disabled, for database '{}'. Use migrate_schemas "
                               "instead. Please read the documentation if you don't know why you "
                               "shouldn't call migrate directly!".format(database))
            
        super(Command, self).handle(self, addrport='', *args, **options)
    
    def check_migrations(self):
        """
        Checks to see if the set of migrations on disk matches the
        migrations in the database. Prints a warning if they don't match.
        """
        print('OK')

The output when i run manage.py, note that the runserver is not present in [saas] commands

Available subcommands:

[auth]
  ..
[debug_toolbar]
..

[django]
  ..

[reversion]
    ...
[saas]
    migrate
    migrate_saas
    migrate_schemas
    sync_schemas
    syncdb
    tenant_command

[sessions]
    ..

[staticfiles]
    collectstatic
    findstatic
    runserver

Collin Anderson

unread,
Oct 6, 2014, 12:34:18 PM10/6/14
to django...@googlegroups.com
try putting the staticfiles app below saas in your INSTALLED_APPS

Carlos Perche

unread,
Oct 6, 2014, 1:06:59 PM10/6/14
to django...@googlegroups.com
great, now it is working.

but i have still a question, why the position in the installed_apps does not affect the migrate command and affect the runserver command ?

thanks

Collin Anderson

unread,
Oct 6, 2014, 1:17:41 PM10/6/14
to django...@googlegroups.com
It's because the staticfiles app is already overriding the built in runserver command, and it's competing with your app. I think it's the only case of a contrib app overriding a built-in command.

The migrate command is built-in to django and there are no contrib apps that override it.
Reply all
Reply to author
Forward
0 new messages