My Objective: To make it so that whenever a record is meant to be deleted, instead it is updated with status = 0. and to also reflect this soft deletion in SELECT & UPDATE queries. i.e. Only affect the records with status 1. handling the deletion part is easy enough, i am stuck on the SELECT & UPDATE part where i need help from you guys.
My Attempt:
Consider the following models and managers.
class StatusFilterManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status=1)
class BaseModel(models.Model):
status = models.PositiveIntegerField(default=1)
objects = StatusFilterManager()
class Meta:
abstract = True
class TestA(BaseModel):
aa = models.IntegerField(null=True)
ab = models.DateTimeField(null=True)
ac = models.TextField(null=True)
class TestD(BaseModel):
da = models.ForeignKey(TestA, on_delete=models.CASCADE)
text = models.TextField(null=True)
This works for basic queries TestA.objects.all()
will give me records with status=1. But it doesn’t work for the following query. TestA.objects.filter(testd__text="hello")
The SQL generated for it is SELECT "app2_testa"."id", "app2_testa"."status", "app2_testa"."aa", "app2_testa"."ab", "app2_testa"."ac" FROM "app2_testa" INNER JOIN "app2_testd" ON ("app2_testa"."id" = "app2_testd"."da_id") WHERE ("app2_testa"."status" = 1 AND "app2_testd"."text" = 'hello') LIMIT 21; args=(1, 'hello'); alias=default
I want an extra “app2_testd”.“status” = 1 in the WHERE clause. What should i do here? Should i change my approach or am i missing something?