How do I create sub-applications in Django?

Django

Django Problem Overview


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.

Django Solutions


Solution 1 - Django

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.

Solution 2 - Django

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

Solution 3 - Django

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.

Solution 4 - Django

Go to your apps folder. Try:

python ../manage.py startapp app_name

Solution 5 - Django

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

Solution 6 - Django

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

Reference: Django Admin documentation

Solution 7 - Django

Use this command in the terminal:

python manage.py startapp application_name

Solution 8 - Django

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjamidaView Question on Stackoverflow
Solution 1 - DjangosebpiqView Answer on Stackoverflow
Solution 2 - DjangoCloudView Answer on Stackoverflow
Solution 3 - DjangoIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 4 - DjangoHorsemouthView Answer on Stackoverflow
Solution 5 - DjangoPramit SawantView Answer on Stackoverflow
Solution 6 - Djangodd42View Answer on Stackoverflow
Solution 7 - Djangomahdi2080View Answer on Stackoverflow
Solution 8 - DjangoIlya ChudakovView Answer on Stackoverflow