Open Elabbasy00 opened 1 year ago
I have the same issue, any solutions?
I have the same issue, any solutions?
I have add the same apps to all tenants type and restrict access on urls
I have a work around
from django.db import connection
from django.db import models
from django.db.models.base import ModelBase
from django_tenants.utils import get_public_schema_name, get_tenant_types
class DjangoTenantSafeDeleteMetadata(ModelBase):
def __new__(cls, name, bases, attrs, **kwargs):
new_class = super().__new__(cls, name, bases, attrs, **kwargs)
original_get_fields = new_class._meta.get_fields
def new_get_field(self, *args, **kwargs):
fields = original_get_fields(*args, **kwargs)
schema_name = connection.schema_name
if settings.HAS_MULTI_TYPE_TENANTS:
apps = set(get_tenant_types().get(connection.schema_name, {}).get("APPS", []))
else:
apps = set(settings.SHARED_APPS if schema_name == get_public_schema_name() else settings.TENANT_APPS)
return [f for f in fields if not f.related_model or f.related_model._meta.app_config.name in apps]
new_class._meta.get_fields = new_get_field.__get__(new_class._meta)
return new_class
class ModelMixin(models.Model, metaclass=DjangoTenantSafeDeleteMetadata):
class Meta:
abstract = True
class Order(ModelMixin):
# your fields here
pass
Try this out it works. I had the same problem I used this as work around.
Should I add the model mixin to every model that has a foreign key to any other model not in his scheme?
I Have a custom user model shared between tenants and public
from the full-version order app, there is an FK to the user model that restrict me to delete any user from the public schema
also, the issue appears in the menu type cuz the order models do not exist yet
any work around this issue?