citusdata / citusdata/django-multitenant
Through model saves are not supported
- Dominant language
- Python
- Stars
- 823
- Forks
- 126
- PR merge metrics
- No merged PRs in 30d
Description
When using a `ManyToManyField `with a through model, the tenant_id is not applied to the through model when the parent instance is saved.
Consider the following:
```
class Store(TenantModel):
tenant_id = 'id'
name = models.CharField(max_length=50)
address = models.CharField(max_length=255)
email = models.CharField(max_length=50)
class Product(TenantModel):
store = models.ForeignKey(Store)
tenant_id='store_id'
name = models.CharField(max_length=255)
description = models.TextField()
class Meta(object):
unique_together = ["id", "store"]
class Purchase(TenantModel):
store = models.ForeignKey(Store)
tenant_id='store_id'
product_purchased = models.ManyToManyField(Product, through=Transaction)
class Transaction(TenantModel):
store = models.ForeignKey(Store)
tenant_id='store_id'
purchase = TenantForeignKey(Purchase)
product = TenantForeignKey(Product)
date = models.DateField()
```
When a Purchase instance is created and/or the product_purchased field is updated, the `tenant_id` is not set for the Transaction.
This appears to be because the `tenant_value` is set in the model save function:
```
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)
return super(TenantModelMixin, self).save(*args, **kwargs)
```
This means it is not called.
Contributor guide
Assessment
This issue has not been assessed yet.