[Django]-How do I create sub-applications in Django?

85👍

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

44👍

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
👤Cloud

16👍

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

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.

9👍

Go to your apps folder. Try:

python ../manage.py startapp app_name

3👍

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

Reference: Django Admin documentation

👤dd42

2👍

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

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.

Leave a comment