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

Try to invalidate m2m in more robust way #94

Merged
merged 2 commits into from
Jul 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions cacheops/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,19 +536,23 @@ def invalidate_m2m(sender=None, instance=None, model=None, action=None, pk_set=N
if not sender._meta.auto_created:
return

for m2m in instance._meta.many_to_many:
if m2m.rel.through == sender:
field_name = m2m.m2m_field_name()
column_name = m2m.m2m_column_name()
reverse_column_name = m2m.m2m_reverse_name()

# TODO: optimize several invalidate_objs/dicts at once
if action == 'pre_clear':
attname = get_model_name(instance)
objects = sender.objects.filter(**{attname: instance.pk})
objects = sender.objects.filter(**{field_name: instance.pk})
for obj in objects:
invalidate_obj(obj)
elif action in ('post_add', 'pre_remove'):
# NOTE: we don't need to query through objects here,
# cause we already know all their meaningful attributes.
base_att = get_model_name(instance) + '_id'
item_att = get_model_name(model) + '_id'
# NOTE: we don't need to query thorugh objects here,
# cause we already know all their meaningfull attributes.
for pk in pk_set:
invalidate_dict(sender, {base_att: instance.pk, item_att: pk})
invalidate_dict(sender, {column_name: instance.pk,
reverse_column_name: pk})


@once
Expand Down
2 changes: 2 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class Labeling(models.Model):
brand = models.ForeignKey(BrandT)
tag = models.IntegerField()

class PremiumBrand(Brand):
extra = models.CharField(max_length=127, blank=True, default='')

# local_get
class Local(models.Model):
Expand Down
15 changes: 15 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,21 @@ def test_base_invalidates_on_remove(self):
lambda: self.bf.labels.remove(self.furious)
)

class MultiTableInheritanceWithM2MTest(M2MTests):

def setUp(self):
self.bf = PremiumBrand.objects.create()
self.bs = PremiumBrand.objects.create()

self.fast = Label.objects.create(text='fast')
self.slow = Label.objects.create(text='slow')
self.furious = Label.objects.create(text='furios')

self.bf.labels.add(self.fast, self.furious)
self.bs.labels.add(self.slow, self.furious)

super(M2MTests, self).setUp()


class M2MThroughTests(M2MTests):
brand_cls = BrandT
Expand Down