StevenC
StevenC

Reputation: 1172

Is there a way to add custom django-admin.py commands that work outside of projects?

I'm trying to write a custom command that works outside of Django projects. I was thinking I could follow the coding patterns of Django's own such commands (e.g., startproject), include my command in an app and install it.

Alas, it seems django cannot see this command, as perhaps it doesn't scan site-packages for custom commands.

Is there a way to make this work or am I sadly correct?

UPDATE: I should note that the goal I was trying to accomplish (writing a command that starts projects based on custom templates) is supported in the coming 1.4 release of Django: https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-startproject (see the --template option).

Upvotes: 0

Views: 863

Answers (3)

PeterH
PeterH

Reputation: 13

I don't quite understand from your post, for what purpose do You want to write such command using Django's manage.py. But suppose you want (as I was) to run some script, that works with Django models, for example. You cannot run such script without setting Django environment.

I do the following:

  1. put my code in script.py

  2. manage.py shell

  3. execfile('script.py')

Maybe, this helps.

Upvotes: 0

ashwoods
ashwoods

Reputation: 2289

You can use a custom manage.py.

You do need a project. A project is, although, nothing more than a python package with a settings.py (and maybe a urls.py file)

So you could just create a project, with whatever commands you want, and in your setup script include a binary script that is nothing more than a manage.py in disguise.

I use it to have a manage.py in the bin path of a virtualenv, but you can call it something else and have that "django" project installed in your system python.

Upvotes: 0

StevenC
StevenC

Reputation: 1172

Based on this code from django.core.management, it does appear that django only searches for project-less commands in its own packages, and will then only find command by scanning INSTALLED_APPS, which means a project is required.

Upvotes: 1

Related Questions