Skip to content

Add the possibility to custom Queryset

Compare
Choose a tag to compare
@louiseGrandjonc louiseGrandjonc released this 17 May 22:39
  • Users can now have their own queryset_class in TenantManager

Here is an example of the usecase

class TaskQueryset(models.QuerySet):
    def opened(self):
        return self.filter(opened=True)

    def closed(self):
        return self.filter(opened=False)

class TaskManager(TenantManagerMixin, models.Manager):
    _queryset_class = TaskQueryset

    def opened(self):
        return self.get_queryset().opened()

    def closed(self):
        return self.get_queryset().closed()


class Task(TenantModelMixin, models.Model):
    name = models.CharField(max_length=255)
    project = TenantForeignKey(Project, on_delete=models.CASCADE,
                               related_name='tasks')
    account = models.ForeignKey(Account, on_delete=models.CASCADE)
    opened = models.BooleanField(default=True)

    objects = TaskManager()

    tenant_id = 'account_id'

  • Clean the delete code to ensure deleting rows only related to current tenant