2

Working in an inherited codebase.

I have a table with 2 objects when I look at it from the API or from the admin site, but it has 3 objects when I do a select * in PostgreSQL.

 name  |            created            | deleted
-------+-------------------------------+------------
 Sweet | 2017-07-21 16:31:28.556949-05 | t
 qwer  | 2017-07-21 16:36:03.096954-05 | t
 asdf  | 2017-07-21 16:35:15.585589-05 | f
(3 rows)

There is a 'deleted' flag in that table which appears to be an automagic soft-deletion flag. If I set the flag true through SQL, it's visible to ORM. If I set the flag false through SQL, it's hidden from ORM.

This particular type of automagic is extremely unhelpful and I want to disable it, but I'd like to understand how this works in Django before I go removing the column.

I am not using anything special to support this behavior. I have found articles which describe how to achieve soft deletion by subclassing special models that support it, but I'm doing none of that. This is just a vanilla subclass of models.Model. I have Googled and searched through the Django docs and I don't see anything that would indicate that just having a "deleted" column gives you soft deletion.

My questions are:

  1. Does Django have some kind of automatic soft deletion support?
  2. If so, where can I find the documentation?
  3. If not, is there some other automatic mechanism I'm not understanding?
Kara
  • 6,115
  • 16
  • 50
  • 57
Rjak
  • 2,097
  • 4
  • 19
  • 24
  • 5
    Django does not have built-in support for soft-deletion. This is most likely implemented using a custom `QuerySet` or `Manager` class. Look for an `objects` attribute on the model class, or anything that is set to a `QuerySet` or `Manager` instance. – knbk Jul 21 '17 at 22:21
  • 1
    @knbk Thank you, that was exactly what was going on. There was a Manager class associated with the model whose get_queryset() method filtered by deleted. – Rjak Jul 21 '17 at 22:26
  • I log my SQL queries in dev mode. You could probably get a better picture of what's going on that way: [https://stackoverflow.com/questions/4375784/log-all-sql-queries](https://stackoverflow.com/questions/4375784/log-all-sql-queries) – Ross Rogers Jul 21 '17 at 23:07

1 Answers1

0

@knbk lead me to the answer:

There was a Manager class associated with the model whose get_queryset() method filtered any queryset operations by the 'deleted' flag.

Rjak
  • 2,097
  • 4
  • 19
  • 24