How do I develop / enable a custom multi-user Django app in a new project? Are there any recommendations?

After the tutorial on Reusable Django Apps, everything works well. But I have a few questions about the development and packaging process for a Django application.

1 - In the tutorial, the application is developed first within the project. It is then copied to a different packaging folder and included in the project again in pip. Is this the way to develop Django apps? For example, if I need to enable new features or fix bugs, should I make changes to the project and then copy and paste them into the package folder outside the project?

2. Assuming 1 is not the only way to develop an application, I started creating a package folder for my application with the following structure:

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

      

After running python3 setup.py sdist

and installing it using, pip3 install --user myApp.tar.gz

I was able to successfully import my app from the new shell of the Django project. But when I run python3 manage.py migrate no tables are created for myApp models. I suppose this is because there is no migrations folder in the myApp package, and as far as I know, the only way to create migrations is to run makemigrations

inside the project. Or am I missing something? Is it possible to generate an initial migration module without having an application in the project?

3. Finally, the main question: when developing an application, should I start a project, copy the application folder for packaging, including its installation, and then continue development in the package folder?

Thanks in advance for any comment or guidance.

PD: Sorry for my english, also the reviews are well received

EDIT 1:

An example to highlight my doubts: After completing the tutorial, the application source code is outside of the project and let's say I need to change the model. I can change them in the application folder, release a new version (like 0.2) and install it. Now, how can I generate migrations for these changes? Should I always have a test project?

+4


source to share


3 answers


Also, a good development workflow is to link the reusable app to your Django project. To easily achieve this, it pip install

has a parameter -e, --editable

which in turn comes from setuptools

design mode .

Reusable Application:

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 virtual access enabled, you can link your reusable app to a project by running:

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

      

Now every change you make to django-myApp

is automatically mapped to my-django-project

without having to first build / package a reusable app.

This becomes convenient in many use cases. For example. imagine the application must be compatible with Python 2.x

and Python 3.x

. When linking, you can set your application to two (or more) different Django settings and run your tests.

+11


source


  • When you package an application, it's because you decided to use it across multiple projects and had some value for it. You can follow the guidelines to ensure your app is reused and packaged when in good condition (minimal functionality works). Ok, you have your application ready, it does what you want, so now you want to separate it from your project? Follow the guide you provided for the folder structure guide and upload it to the repository. Add some tests as well, they should be. Now you can use a couple of approaches to bring them back to your project.

    • Package it and install it from tar.gz via pip (or directly from the repository)
    • Cloning your repository as a submodule
    • Keep a copy in your project and manually update the changes in the repository

I'm not a fan of the third. The second option has its caveats, but it might work. I definitely prefer the former.

Create a test project around your new application and use it to develop its new features (with minimal testing requirements), update your repository and finally update the application in your project via pip. Now your application is independent of your project, so don't tie it to it.

  1. Have you added it to your INSTALLED_APPS? If you did, check that your models.py is available ... try using the django shell. Try building a migration module to see if it works, which shouldn't take long.

  2. Finally, the answer is: No, your application is now different from another, now you continue to develop in the repository as you would with any other project and then update as with any other application that got a new version released. You don't need (and shouldn't) touch your virtual folders at all.



These are the steps to update in your application:

  • You have encountered a nasty bug or thought of some nice feature.
  • You've added tests to cover it.
  • You've updated your code to improve your application.
  • Repeat the last steps until your application is stable again.
  • If your app is in Pypi, you release a new package and update it there, otherwise you update it from the repository.

This obviously takes longer than developing an application within your project, but it also helps to ensure good quality and will not create problems for your projects.

+3


source


I ran into a similar situation while developing this library ( django-nsync ).

I solved the "problem" by creating a makemigrations.py

script that sets the minimum Django settings to enable the "application" and then makes a command call makemigrations

.

The script looks like this:

def make_migrations():
    from django.core.management import call_command
    call_command('makemigrations', '<YOUR APP NAME>')


if __name__ == '__main__':
    import sys
    # I need to add the 'src' folder to PYTHON PATH
    sys.path.append('./src/')

    try:
        from django.conf import settings

        settings.configure(
            INSTALLED_APPS=[
                '<ANY APPS YOUR APP DEPENDS ON>',
                '<YOUR APP NAME>',
            ],
        )

        import django
        django.setup()

    except ImportError:
        import traceback
        traceback.print_exc()
        raise ImportError('To fix this error, sort out the imports')

    make_migrations()

      

This script does not require a separate project to create migrations.

NB: I tried to link it to the setup.py files so that whenever you try to build (or more importantly free) the library it checks that the migration is up to date / in source control. However, I gave up because:

  • I hope you don't have to do much for the NSync library.
  • It quickly got messy (like checking the number of files in the migration folder, then running the migration, then checking again, then deleting the files or keeping them? Should I use this at release time? Should I check with Git how bumpversion does? blah ....)
+1


source







All Articles