0

I have an application which is made of several apps. The thing is that by default, only 1 app is installed. I called it 'base'. This base app enables the user to manage users, groups (but that isn't the point here) AND should enable users to install other apps on runtime (that is the point).

I created an Application model. In my UI, I list all the applications with a button install aside of it.

The idea is, when I click on this button, to :

  • Mark the application as installed
  • Add it to the App registry on runtime (that's the blocking point)
  • Apply the migration for this app

So here are some samples of code :

My Application model :

from django.apps import apps
from django.conf import settings
from django.core import management
from django.db import models


class Application(models.Model):

  class Meta:
      db_table = 'base_application'

  name = models.CharField(max_length=255, unique=True)
  verbose_name = models.CharField(max_length=255)
  version = models.CharField(max_length=255, null=True, blank=True)
  summary = models.CharField(max_length=255)
  description = models.TextField(null=True, blank=True)
  is_installed = models.BooleanField(default=False)
  is_uninstallable = models.BooleanField()

My install() method would look like :

I know there are mistakes in it and that is the point of this question. I don't understand how the App Registry actually works.

Note that I must provide the reverse of it too (uninstall)

    def install(self):
      self.is_installed = True
      self.save()
      settings.INSTALLED_APPS.append(self.name)
      apps.populate(settings.INSTALLED_APPS)
      self.migrate()

My migrate() method :

    def migrate(self):
        management.call_command('makemigrations', self.name, interactive=False)
        management.call_command('migrate', self.name, interactive=False)

The usual error I get is No installed app with label ....

Thanks in advance for your help. I can precise if needed.

lbris
  • 1,068
  • 11
  • 34
  • 1
    Check this related question: https://stackoverflow.com/questions/24027901/dynamically-loading-django-apps-at-runtime – Dev Catalin Feb 06 '20 at 17:18
  • Yeah I've already read it many times and I still can't make my mind on it. That's why I asked here, considering I'm doing something completely silly that someone would tell me. – lbris Feb 06 '20 at 17:23
  • @CatalinHoha Well finally I made it work but how do I know if `apps_ready` `models_ready` `loading` and `ready` have to be `False` ? I've seen people only setting `ready` to `False`. This part of Django is not really well documented in my opinion. – lbris Feb 10 '20 at 10:02

0 Answers0