Login

Automigrate, autocreatesuperuser if not User.count() in runserver and use manage.py:main as entrypoint

Author:
jpic
Posted:
September 17, 2017
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

With this awesome manage.py, it will try to migrate first when called with runserver.

Also, this manege.py has super power to be used in your entry point as such:

entry_points = {
    'console_scripts': [
        # u haz a setup.py -> u haz importable module :) 
        'yourcommand = yourproject.manage:main',
    ],
},

Example output:

$ yourcommand runserver
Operations to perform:
  Apply all migrations: auth, contenttypes, admin, sessions
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying sessions.0001_initial... OK
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.
Performing system checks...
Username (leave blank to use 'jpic'): 
Email address: [email protected]
Password: 
Password (again): 
Superuser created successfully.
Welcome in 2017, where automation is king

System check identified no issues (0 silenced).
September 17, 2017 - 21:21:41
Django version 1.9.10, using settings 'crudlfap_example.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Extra note: it doesn't matter if migrate crashes for now, since django runserver doesn't support not being able to connect to database. So most of the time I hope to shouldn't need to disable this hack.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""Manage.py with auto migrate for runserver., also usable as entry_point"""

#!/usr/bin/env python
import os
import sys

def main():
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "YOUR_PROJECT.settings")
    from django.core.management import call_command, execute_from_command_line
    if sys.argv[1] == 'runserver':
        import django
        django.setup()
        call_command('migrate')

        from django.conf import settings
        from django.apps import apps
        User = apps.get_model(settings.AUTH_USER_MODEL)
        if not User.objects.count():
            call_command('createsuperuser')

    execute_from_command_line(sys.argv)

if __name__ == "__main__":
    main()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.