CleitonDeLima / django-easy-tenants

This is a Django app for managing multiple tenants on the same project instance using a shared approach.
MIT License
49 stars 9 forks source link

Only set tenant in TenantManager.bulk_create if tenant exists #99

Closed mortenthansen closed 7 months ago

mortenthansen commented 9 months ago

Change from:

    def bulk_create(self, objs, *args, **kwargs):
        tenant = get_current_tenant()

        for obj in objs:
            setattr(obj, field_name, tenant)

        return super().bulk_create(objs, *args, **kwargs)

to

    def bulk_create(self, objs, *args, **kwargs):
        tenant = get_current_tenant()

        if tenant:
            for obj in objs:
                setattr(obj, field_name, tenant)

        return models.Manager.bulk_create(self, objs, *args, **kwargs)

My use-case is the use of bulk_create with tenant_context_disabled() where I control the tenant myself with the objects parsed to bulk_create.