Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lmbak opened this issue Jun 1, 2021 · 3 comments
Open

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

lmbak opened this issue Jun 1, 2021 · 3 comments

Comments

@lmbak
Copy link

lmbak commented Jun 1, 2021

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
Copy link
Member

Gagaro commented Jun 7, 2021

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)

caiocarrara added a commit to techmatters/terraso-backend that referenced this issue Jan 25, 2022
There was a specific case where using Django built-in lookups for
relationships doesn't work properly with the soft delete solution we
implemented with `django-safedelete`. According to the library
maintainers, it's a known limitation[1].

To work around this limitation, a customized filter set were created for
Group schema on GraphQL. This custom filter set implements the
adjustments expected to ignore soft deleted objects when filtering by
relationships (joins).

[1] - makinacorpus/django-safedelete#158
caiocarrara added a commit to techmatters/terraso-backend that referenced this issue Jan 25, 2022
There was a specific case where using Django built-in lookups for
relationships doesn't work properly with the soft delete solution we
implemented with `django-safedelete`. According to the library
maintainers, it's a known limitation[1].

To work around this limitation, a customized filter set were created for
Group schema on GraphQL. This custom filter set implements the
adjustments expected to ignore soft deleted objects when filtering by
relationships (joins).

[1] - makinacorpus/django-safedelete#158
caiocarrara pushed a commit to techmatters/terraso-backend that referenced this issue Jan 25, 2022
There was a specific case where using Django built-in lookups for
relationships doesn't work properly with the soft delete solution we
implemented with `django-safedelete`. According to the library
maintainers, it's a known limitation[1].

To work around this limitation, a customized filter set were created for
Group schema on GraphQL. This custom filter set implements the
adjustments expected to ignore soft deleted objects when filtering by
relationships (joins).

[1] - makinacorpus/django-safedelete#158
@shivananda-sahu
Copy link

@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
Copy link
Member

Gagaro commented May 24, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants