LucasRoesler / django-archive-mixin

Archive / safe delete Django models
MIT License
18 stars 5 forks source link

Soft deletes ineffective on models.ManyToManyField relations with through table #2

Open GavG opened 5 years ago

GavG commented 5 years ago

When implementing a ManyToMany relation with a 'through' table in an effort to add soft deletes across the pivot, the related objects returned from the related manager are not filtered out by their soft delete status. A basic example:

class Person(ArchiveMixin, models.Model):

    events  = models.ManyToManyField('Event', through='Participant', related_name='participants')

The Participant model also implements the ArchiveMixin:

class Participant(ArchiveMixin, models.Model):

An instance or Person with 9 participation (3 soft deleted) will return all 9 on calls to .events

>>> person.events.count()
9

As a workaround we can create an accessor method such as:

    @cached_property
    def get_events(self):
        return Event.objects.filter(participant__deleted_on__isnull=True, participant__passmanager_person_id=self.id)

Which will return correct results

>>> person.get_events.count()
6

However, this is not ideal. I believe the problem could be that Django is using a different manager for the ManyToMany relations that is not considered by this mixin. I've had a dig around to see if I could extend the functionality to cover this scenario but I cannot find where this should go. Any insight on this would be appreciated.

Many thanks.

viniciuscb commented 5 years ago

Hi. it seems that this package is not being updated. I am working on a fork, please check it out at https://github.com/coletivoEITA/django-archive-mixin