citusdata / django-multitenant

Python/Django support for distributed multi-tenant databases like Postgres+Citus
MIT License
712 stars 117 forks source link

support for uuid #69

Closed miguelrosalesmtl closed 4 years ago

miguelrosalesmtl commented 4 years ago

Hi, I tried using uuids as ids and it doesn't work because of the save method in TenantMixin. When saving the setattr(self, self.tenant_field, tenant_value) line never gets called because self.pk has value when dealing with uuids different from IntegerField. I created my own abstract class and added a created field it did the trick, but it would be nice to have native support for uuids.

def save(self, *args, **kwargs):
    tenant_value = get_current_tenant_value()
    if not self.pk and tenant_value and not isinstance(tenant_value, list):
        setattr(self, self.tenant_field, tenant_value)
s4ke commented 4 years ago

We fixed it by using this implementation in our model base class:

    def save(self, *args, **kwargs):
        tenant_value = get_current_tenant_value()
        if self._state.adding and tenant_value and not isinstance(tenant_value, list):
            setattr(self, self.tenant_field, tenant_value)

        return super().save(*args, **kwargs)
miguelrosalesmtl commented 4 years ago

my man. :+1:

louiseGrandjonc commented 4 years ago

Hi @miguelrosalesmtl, @s4ke,

thank you for raising this issue. I created a branch with a fix for this: https://github.com/citusdata/django-multitenant/tree/fix-uuid-tenant-set

If you want to try it before we publish the fix on pypi, it would be great.

johnnywell commented 4 years ago

there's already a release for that?

louiseGrandjonc commented 4 years ago

Hi,

I just released a new version 2.2.1 with the fix for this.