Making queries¶
In short, the queries are the same as django's queries. The difference is that by default behavior all soft deleted instances are excluded.
To make a querry and include the deleted instance just need to give parameter with_deleted
to the querry. This is a boolean parameter, so it can be True or False.
Info
Soft deleted is an instance where the filed deleted_at
is not None
All¶
1 2 |
|
1 2 |
|
1 2 |
|
As you can see, .all()
will return the same instances that all(with_deleted=False)
On related_name queries
When an instance has been soft deleted, the related_querry all()
will return
with_deleted=True
by default.
That happens ecause if the instance has been soft deleted it want's to be querried the deleted objects,
BUT you can alway use the parameter with_deleted=False
and it all soft deleted are excluded.
It's something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Why paranoid does it
The explanation why Paranoid Query does it, is because imagine we have a person and we have 2 phones related to that person, and that person has been soft deleted, and by cascade person's phones also soft deleted.
Now imagine that in the future, that person wants a report of your datas once saved in database, so when we filter his data, we will need, also, his data deleted.
That is why paranoid query will include soft deleted when querring related_name with a soft delete instance.
Filter¶
1 2 |
|
1 2 |
|
1 2 |
|
As you can see, .filter(**kwargs)
will return the same instances that filter(with_deleted=False, **kwargs)
Deleted_only¶
To filter only deleted you must use deleted_only
filter. Thats because filter
override querry parameter deleted_at
and change it.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Failure
Do not do that
1 |
|
The param deleted_at__isnull
is overwritten by querry filter.
That happens because every param wich starts with deleted_at
are removed
Get¶
1 2 |
|
Careful with get() method, because it can raise some errors. The possible raises are:
-
model.DoesNotExist: (Django) will be raised if the querry doesn't match to any instance
-
model.MultipleObjectsReturned: (Django) will be raised if more than 1 instances matches with the querry
-
model.SoftDeleted: will be raised if the instance has been soft deleted.
You can do the following:
1 2 3 4 5 |
|
or
1 2 3 4 5 6 |
|
But, if you pay attention it doesn't allow you to get an instance that has been soft deleted. Don't worry, no need to cry! get_deleted
and get_or_restore
will save you!
Get_deleted¶
1 2 |
|
Careful with get_deleted() method, because it can raise some errors. The possible raises are:
-
model.DoesNotExist: (Django) will be raised if the querry doesn't match to any instance
-
model.MultipleObjectsReturned: (Django) will be raised if more than 1 instances matches with the querry
-
model.IsNotSoftDeleted: will be raised if the instance has not been soft deleted yet.
You can do the following:
1 2 3 4 5 |
|
or
1 2 3 4 5 6 |
|
Get_or_restore¶
This method will work just like Django's wiht a thiny difference, it will restore the instance if it has been soft deleted
1 |
|
Like all get method, it can raises some exceptions:
-
model.DoesNotExist: (Django) will be raised if the querry doesn't match to any instance
-
model.MultipleObjectsReturned: (Django) will be raised if more than 1 instances matches with the querry
1 2 3 4 5 |
|
or
1 2 3 4 5 |
|
Restore¶
This method restore all the instances soft deleted int the current querry set. Look at the example bellow
1 2 3 4 5 6 7 8 9 10 11 12 |
|