makinacorpus / django-safedelete

Mask your objects instead of deleting them from your database.
https://django-safedelete.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
682 stars 122 forks source link

Filtering with double underscores does not seem to work as expected #158

Open lmbak opened 3 years ago

lmbak commented 3 years ago

Working with Django's .filter(foo__bar) method does not seem to filter out the soft deleted models. Is that desired behaviour?

Say we have these two models:

class Parent(SafeDeleteModel):
    name = CharField()

class Child(SafeDeleteModel):
    parent = ForeignKey('app.Parent',related_name='children')
    name = CharField()

And we do:

peter = Parent(name='Peter')
may = Child(name="May", parent=peter) 

may.delete()

# Now get the parents with a child called "May", I would expect no parents to be found
# but there are, parents is not empty, we acutally get peter
parents = Parent.objects.filter(children__name='May')

Is this expected behaviour?

Gagaro commented 3 years ago

Indeed this is not handled. You would need to explicitly exclude the deleted instances:

parents = Parent.objects.filter(children__name='May', children__deleted__isnull=False)
shivananda-sahu commented 2 years ago

@Gagaro do you have thoughts on how this can be implemented? Would this require adding support would involve overriding the filter /other sql function implementations. Happy to contribute if there is a preferred path forward.

Gagaro commented 2 years ago

I'm not sure how the lookups are done for relations. I'm pretty sure this should not be done in the queryset or query, but at the field / descriptor level, this may require a new ForeignKey / etc type?

There may be something to be done with the base manager, but I think this may cause more issues down the line.