bernardopires / django-tenant-schemas

Tenant support for Django using PostgreSQL schemas.
https://django-tenant-schemas.readthedocs.org/en/latest/
MIT License
1.45k stars 424 forks source link

Signal not called #603

Closed maxthevoice closed 5 years ago

maxthevoice commented 5 years ago

I have an issue since I installed the library. The signals are not called anymore. When creating a User, for example, a Profile instance should be created, but it doesn't.

signals.py:

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        models.Profile.objects.create(user=instance)

tests.py:

class TestCreateUserProfile(TenantTestCase):
    def test_create_user_profile(self):
        user = UserFactory()  # Factory boy
        self.assertIsInstance(user.profile, models.Profile)

settings.py:

SHARED_APPS = (
    'tenant_schemas',
    'dal',
    'dal_select2',

    'django.contrib.contenttypes',

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.postgres',

    'allauth',
    'allauth.account',
    'corsheaders',
    'django_cleanup',
    'django_extensions',
    'debug_toolbar',
    'localized_fields',
    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',
    'simple_history',
    'versatileimagefield',
    'timezone_field',
    'ordered_model',
    'django_filters',
    'phonenumber_field',
    'djcelery_email',
    'mjml',

    'core',
    'core_project',
    'two_factor_auth',
    'users',
    'controls',
    'occurrences',
    'organizations',
)

TENANT_APPS = (
    'django.contrib.contenttypes',

    'controls',
    'occurrences',
)

INSTALLED_APPS = list(SHARED_APPS) + [app for app in TENANT_APPS if app not in SHARED_APPS]

TENANT_MODEL = 'organizations.Organization'

DATABASES = {
    'default': {
        'ENGINE': 'psqlextra.backend',
        'NAME': env.str('POSTGRES_DB'),
        'USER': env.str('POSTGRES_USER'),
        'PASSWORD': env.str('POSTGRES_PASSWORD'),
        'HOST': env.str('POSTGRES_HOST', 'postgres'),
        'PORT': 5432,
    },
}

ORIGINAL_BACKEND = 'django.db.backends.postgresql'
POSTGRES_EXTRA_DB_BACKEND_BASE = 'tenant_schemas.postgresql_backend'

DATABASE_ROUTERS = (
    'tenant_schemas.routers.TenantSyncRouter',
)

I tried with tenant_schemas.postgresql_backend directly as the main ENGINE, but I got the same issue. So I don't think it's related with psqlextra.

I tried it directly from the admin, and I have the same issue.

Let me know if you need more details.

maxthevoice commented 5 years ago

It's probably related to the fact that we need to use the name of the app instead of the relative path users.apps.UsersConfig.

I think https://github.com/bernardopires/django-tenant-schemas/pull/569 will fix this issue.

apps.py

class UsersConfig(AppConfig):
    name = 'users'

    def ready(self):
        import users.signals

The signals are not imported.

maxthevoice commented 5 years ago

I used Django Tenants library instead. The import of apps is better and don't interfer with signals.