citusdata / citusdata/django-multitenant
Tenant not filtered in model forms
- Dominant language
- Python
- Stars
- 823
- Forks
- 126
- PR merge metrics
- No merged PRs in 30d
Description
Suppose I have the following 2 models and a model form
`
class CostCenter(ClientModelMixin, models.Model):
tenant_id = 'client_id'
client = models.ForeignKey('accounts.Client', on_delete=models.CASCADE)
name = models.CharField(max_length=50)
objects = TenantManager()
class Account(ClientModelMixin, models.Model):
tenant_id = 'client_id'
client = models.ForeignKey('accounts.Client', on_delete=models.CASCADE)
name = models.CharField(max_length=50)
cost_center = TenantForeignKey(CostCenter, on_delete=models.PROTECT)
objects = TenantManager()
class CreateForm(forms.ModelForm):
class Meta:
model = Account
fields = ['cost_center']
`
When the model form is rendered it shows all cost centers in the dropdown. I think it happens because the model form is created before the tenant is set and then no tenant filtering is applied.
I can override the init method of every form and set the queryset correctly but this isn't ideal.
How can this be solved?
Contributor guide
Assessment
This issue has not been assessed yet.