By code below in admin panel query set return only rows that is_deleted is "false" if I want to return all rows. I have one idea but not sure that is bests or does not have a bug.
all models inherit from this model
class BaseModel(models.Model):
is_deleted = models.BooleanField(default=False, verbose_name=_("is Deleted"))
objects = BaseModelManager()
...
and I add filter (is_deleted=false) for all query by the below manager(I can not delete this code because all APIs use this code)
class BaseModelManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(is_deleted=False)
for example, a model book is
class Book(BaseModel):
title = models.CharField(max_length=150)
...
In the app admin
@register(Book)
class BookAdmin(BaseAdminModel):
list_display = ('title',)
list_filter = ('is_deleted',)
class BaseAdminModel is
class BaseAdminModel(ModelAdmin):
list_display = ('is_deleted',)
my idea is to change the "base model" and "base admin model" like this
class BaseModelManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(is_deleted=False)
class AdminModelManager(models.Manager):
def get_queryset(self):
return super().get_queryset().all()
class BaseModel(models.Model):
is_deleted = models.BooleanField(default=False, verbose_name=_("is Deleted"))
objects = BaseModelManager()
admin_objects = AdminModelManager()
...
class BaseAdminModel(ModelAdmin):
list_display = ('is_deleted',)
def get_queryset(self, request):
return self.model.admin_objects.all()
I personally would go with your option two solution. So attaching two managers to the model, one for deleted objects the other showing all objects.
Then in your admin you override the
get_queryset
function. This will be the cleanest and simplist solution going forwards.