-
Notifications
You must be signed in to change notification settings - Fork 122
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
Invalid result in many to many list if through instance is soft deleted #172
Comments
Hi, was it with the 1.1.0? Filtering was moved to the query itself to fix those kind of things. |
I'm seeing this same issue with school.students.count() # returns 1
school.schoolstudent_set.count() # returns 0 |
Thanks for your response. |
Thanks for your response. |
It's probably because the through instance is deleted but not the related one. I'll try to see if I can find a quick fix, |
So because I added a test which reproduce this but it fails for now. |
Correct. As a workaround for your situation, you can just do something like:
But it will be difficult to remember not to use I'll keep looking at this and see if I can find any solution, and if so submit a PR. I decided to adopt this library in a project I'm working on, largely because of its strong tests and handling of tricky situations like this, so I'd love to have this one handled in an intuitive way. Thanks for your work & responsiveness @Gagaro |
A bit late to the party, just chiming in here since I encountered the same issue. Unfortunately, I don't see a way to easily integrate a fix for this into the current code base, since the problem is due to the ManyRelatedManager bypassing the manager for through models. But a customizable solution that worked for me was just to superclass the get_queryset method on the managers for the models at both ends of the m2m field. So for your example it would look like:
And the manager for the School class would look much the same. I dunno, lmk if this is poor Django form. I'm on Django 4.1.1, in case that matters |
I'm also seeing the same behaviour. As far as I can tell this makes the safedelete package basically incompatible with m2m relationships with through models? At the moment we have been able to get something that works by generalising @charles-hussey's solution so that it can handle being passed to all models, and with any custom foreign key related names that might have been set on the through models.
I'd love to hear if anyone else has solved this in a different way, or how people are still using Django's m2m fields with safedelete. Also, if anyone knows of any issues we may have introduced generally into the ORM by using this custom manager with all models please let me know! |
I tried this solution and it works only if no prefetch was made. If a prefetch was used, the core_filters updated in that piece of code are not used and you get all instances, no matter if the through model was safe deleted or not. I debug the code and this is what I found: The query was being returned in that 1084 line (from the prefetch cache) instead of the queryset with the filters applied. We ended up using a custom prefetch (no the basic |
Hi guys. I have below models
I create a relationship between a school and a student, and then I delete it. but the below event occurs
As you see. we get wrong results. we deleted the relationship but we get the queryset.
Have you got any idea to solve the issue?
The text was updated successfully, but these errors were encountered: