Search code examples
djangopython-3.xpackagedjango-appsdjango-1.8

How to develop/include a Django custom reusable app in a new project? Are there some guidelines?


Following tutorial on Django reusable apps things work fine. But I have some questions about the process of developing and packaging a Django app.

1 - In the tutorial, the app is developed first within a project. Then, it is copy-pasted out in another folder for packaging and then included again in the project vía pip. Is this the way for developing Django apps? For example, if I have to include new features or fix bugs, Should I make changes in the project and then copy-paste them to the package folder outside the project?

2 - Assuming that 1 is not the only way to develop an app, I started creating a package folder for my app with this structure:

django-myApp
|--myApp
|  |--models
|     |--file1.py
|     |--file2.py
|--setup.py
|--README.rst

After running python3 setup.py sdist and installing it with pip3 install --user myApp.tar.gz I can successfully import my app from a new Django project shell. But when I run python3 manage.py migrate, tables for models of myApp are not created. I guess it is because there is not migration folder in myApp package, and as far as I know, the only way to create migrations is running makemigrations within a project. Or Am I missing something basic? Can I generate an initial migration module without having the app in a project?

3 - Finally, the main question is: When developing an app, Should I have to start a project, copy out the app folder for packaging, reincluding it vía installing, and then continue developing in the package folder?

Thanks in advance for any comment or guidance.

P.D.: Sorry for my English, also comments about it are well-received

EDIT 1:

An example to highlight my doubt: After finish tutorial, App source code is outside the project and suppose I need to change models. I can change them in App folder, release a new version (e.g. 0.2) and install it. Now, How can I generate migrations for these changes? Should I always have a test project?


Solution

  • Additionally, a good workflow during development is to link the reusable app into your Django project. To easily achieve this pip install has the -e, --editable option, which in turn derives from the setuptools Development mode.

    Reusable app:

    django-myApp
    |--myApp
    |  |--models
    |     |--file1.py
    |     |--file2.py
    |--setup.py
    |--README.rst
    

    Django setup:

    my-django-project
    |--my_django_project
    |  |--settings.py
    |  |--urls.py
    |  |--wsgi.py
    |  |--...
    |--manage.py
    

    With your virtualenv activated you can now link your reusable app to the project by running:

    (myvenv) $ pip install --editable /path/to/django-myApp
    

    Now, every change that you make in django-myApp is automatically reflected in my-django-project without the need to build/package the reusable app first.

    This becomes convenient in many use cases. E.g. imagine developing the app to be compatible with Python 2.x and Python 3.x. With linking, you can install the app into two (or more) different Django setups and run your tests.