70

I'm a Django newbie, but fairly experienced at programming. I have a set of related applications that I'd like to group into a sub-application but can not figure out how to get manage.py to do this for me.

Ideally I'll end up with a structure like:

project/
   app/
       subapp1/
       subapp2/

I've tried manage.py startapp app.subapp1 and manage.py startapp app/subapp1
but this tells me that / and . are invalid characters for app names.

I've tried changing into the app directory and running ../manage.py subapp1 but that makes supapp1 at the top level. NOTE, I'm not trying to directly make a stand-alone application. I'm trying to do all this from within a project.

jamida
  • 2,869
  • 5
  • 25
  • 22
  • 3
    Could you let us know why you are trying to do this? What is app and what are subapp1 and subapp2? A django app is a collection of python modules (and, as Ignacio points our, python packages) that represent a complete web application. I can't figure what you are trying to do or why people are upvoting your question... – cethegeek May 19 '10 at 03:30
  • My "need" can be described by extending the standard Book example to also include Genre. If you imagine that each Genre (SF, Comedy, Drama, non-fiction) has additional database fields beyond the standard Book fields. I'm new enough to Python that I don't really care about class inheritance (eg class SFBook(Book)), replicating the little bit of common code will be fine for the initial version of this project. But I am interested in organizing all the genres under a common directory instead of having them all at the top project level. – jamida May 19 '10 at 16:06
  • 5
    Hi Celopes. Thanks. I understand how bad my approach is. I'm a reasonably experienced designer/developer, but I am very new to python/django and this is a prototype project. I want to get something off the ground first, I plan to throw the guts of this away after everyone gets a chance to see the outside. To that end I may just let the clutter in the parent directory survive. I figure I can learn django/python while taking this "phenomenally bad" approach and in the process learn enough of how python classes and modules work to be able to redesign it correctly later. – jamida May 24 '10 at 15:53
  • No need to create a "sub" app to include "genre." If I understand your comment correctly and what you are trying to achieve, it seems that you need to add tables for "Genre" in your models. It appears to be more of a relational database approach needed than adding a sub app. – Robin Aug 12 '22 at 01:24

9 Answers9

84

You can still do this :

cd app
django-admin startapp subapp1

This will work (create the application basic structure), however app and subapp1 will still be considered as two unrelated applications in the sense that you have to add both of them to INSTALLED_APPS in your settings.

Does this answer your question ? Otherwise you should tell more about what you are trying to do.

sebpiq
  • 7,540
  • 9
  • 52
  • 69
  • 2
    This is how it's done, but usually the command is called `django-admin.py`. Also, in `INSTALLED_APPS` you will need to put `project.app` and `project.app.subapp1` etc. – Rob Golding May 19 '10 at 09:15
  • For some reason, with my version of django it is called `django-admin` without `.py`, but on some other computers, I have to use `django-admin.py`... – sebpiq May 19 '10 at 09:30
  • 2
    @sebpiq `django-admin` is conveniently used in *Ubuntu* as shortcut for `django-admin.py`, if that is the case on your computer. – nerdoc Jan 10 '14 at 05:40
  • How about if I have created the app01 and app02, then how can I put the app02 into app01 now? – qg_java_17137 Nov 16 '17 at 09:42
  • @sebpiq If use your method to create the app, then in the settings. INSTALLED_APPS should add `app.subapp1 `, right? – user7693832 Nov 21 '17 at 02:38
43

According to Django documentation,

If the optional destination is provided, Django will use that existing directory rather than creating a new one. You can use ‘.’ to denote the current working directory.

For example:

django-admin startapp myapp /Users/jezdez/Code/myapp

So, you can do it by this method:

  1. Create sub_app1 directory in app directory
  2. python manage.py startapp sub_app1 app/sub_app1
Community
  • 1
  • 1
Cloud
  • 2,859
  • 2
  • 20
  • 23
  • 6
    This should be the accepted answer. The proper way of creating new app with `manage.py`: `python manage.py startapp name [directory]` where `name` is the app name and there is an optional argument directory. From the help: *Creates a Django app directory structure for the given app name in the current directory or optionally in the given directory.* – cezar Jan 16 '18 at 09:21
15

Its Simple

Step 1 - Create Project

django-admin startproject app
cd app

Step 2 - Create api folder

mkdir api
cd api
touch __init__.py
cd ..

Step 3 - Create nested apps

python manage.py startapp user ./api/user
python manage.py startapp post ./api/post
python manage.py startapp comment ./api/comment

Step 4 - Register nested apps

INSTALLED_APPS = [
    ...
    'api.user',
    'api.post',
    'api.comment',
]

Step 5 - Change name of Apps

Update the name in apps.py files in all three apps

class UserConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'api.user'

class PostConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'api.post'

class CommentConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'api.comment'   

 

and We are done!

Note : Step 2 is important

Pramit Sawant
  • 682
  • 1
  • 9
  • 17
14

Django doesn't support "subapplications" per se. If you want code to be collected in packages within an app then just create them yourself. Otherwise you're just asking for pain.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
9

Go to your apps folder. Try:

python ../manage.py startapp app_name
Horsemouth
  • 91
  • 2
  • 3
3
django-admin.py startapp myapp /Users/jezdez/Code/myapp

Reference: Django Admin documentation

Ryan Allen
  • 5,414
  • 4
  • 26
  • 33
dd42
  • 138
  • 3
1

In order to create a nested app in the Django project, you have to follow the following steps:

  1. Create a subdirectory in the parent directory where you want to add a sub-app.
mkdir finances/expences

N.B: Here finances is the existing parent app and expenses will be the next nested sub-app.

  1. If the subdirectory is created successfully then run the following command in the terminal.

python manage.py startapp expenses finances/expenses

Please check the expenses subdirectory is now looks like a Django app and will behave like that.

Don't forget to register this app in the settings file and change the name from the config file of the app expenses, like -

class ExpensesConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'finances.expenses'

Here, the name was only 'expenses' but it has been changed to 'finances.expenses' otherwise no migrations will be applied.

0

Use this command in the terminal:

python manage.py startapp application_name
allexiusw
  • 1,533
  • 2
  • 16
  • 23
mahdi2080
  • 11
  • 2
0

there is really no any differ if you use django-admin or manage.py in this case - both will create https://docs.djangoproject.com/en/4.0/ref/django-admin/#django-admin-and-manage-py

In addition, manage.py is automatically created in each Django project. It does the same thing as django-admin but also sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.