django_manage module detailed explaination

The module provide here
http://docs.ansible.com/django_manage_module.html
needs to have some more example for manage.py command.

example: createsuperuser. How can we create superuser with a specific username and password? is there any way to pass this information in this module

`

  • name: Django Create Superuser


django_manage: command=createsuperuser
app_path={{app_path}}
settings={{django_settings}}
tags:

  • django

`

and how do we use latest django’(1.7)'s makemigration command where we have to give app name (I mean
python manage.py makemigration app1
)

for same environment/path settings collectstatic command works perfectly

`

  • name: Django Collectstatic
    django_manage: command=collectstatic
    app_path={{app_path}}
    settings={{django_settings}}

`

Could somebody please provide example of these

Thanks

The createsuperuser command doesn’t support a command line option to set a password; you can only set username and email when running with the --noinput flag. There are hackish workarounds for creating an initial superuser with password (http://source.mihelac.org/2009/10/23/django-avoiding-typing-password-for-superuser/), but these wouldn’t use the django_manage module. Your other option would be writing your own custom management command for creating superusers.

For running django management commands not specifically listed in the docs, or to use custom command options, you can specify your command and options as a single string, e.g.:

  • django_manage: command=“makemigrations --noinput app1” app_path={{app_path}} settings={{django_settings}}

Thanks Chris

Your solution for makemigrations command working perfect but I didn’t find similar thing in docs. have I missed something?
I’ve two more small queries.

  1. how do I pass multiple app name in makemigrations command which is in a variable (I mean using ansible’s with_items)

`

  • name: Django makemigrations
    django_manage: command=“makemigrations --noinput app1” app_path={{app_path}} settings={{django_settings}}

with_items:

  • app1
  • app2
  • app3

`

  1. how do I pass username and password in createsuperuser command?
    I run this task but it didn’t work?

`

  • django_manage: command=“createsuperuser --noinput myuser myemail@xyz.com mypassword” app_path={{app_path}} settings={{django_settings}}

`

Thanks again for –input thing :slight_smile:

To use with_items, replace app1 in your command with the variable {{item}}:

- django_manage: >
    command="makemigrations --noinput {{item}}"
    app_path={{app_path}}
    settings={{django_settings}}
  with_items:
    - app1
    - app2
    - app3

The createsuperuser command doesn't support setting a password from the
command line. I've found a simple third-party app (
https://github.com/ActiveState/django-stackato) to add a management command
for changing a user's password, so after installing it you could run
something like:

- django_manage: >
    command="createsuperuser --noinput --username=myuser --email=
myemail@xyz.com"
    app_path={{app_path}}
    settings={{django_settings}}
- django_manage: >
    command="changepassword2 myuser mypassword"
    app_path={{app_path}}
    settings={{django_settings}}

The django_manage documentation does indicate that "Other commands can be
entered, but will fail if they're unknown to Django.", but it doesn't give
an example of running one that's not in the list of built-in commands.
I've submitted a fix (
https://github.com/ansible/ansible-modules-core/pull/574).