Add index and query reduction in clubs

This commit is contained in:
Skia 2017-05-20 12:36:18 +02:00
parent 2f5bd7d2ef
commit e689f7f1db
2 changed files with 31 additions and 1 deletions

View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('club', '0007_auto_20170324_0917'),
]
operations = [
migrations.AlterField(
model_name='club',
name='id',
field=models.AutoField(primary_key=True, serialize=False, db_index=True),
),
]

View File

@ -39,6 +39,7 @@ class Club(models.Model):
""" """
The Club class, made as a tree to allow nice tidy organization The Club class, made as a tree to allow nice tidy organization
""" """
id = models.AutoField(primary_key=True, db_index=True)
name = models.CharField(_('name'), max_length=64) name = models.CharField(_('name'), max_length=64)
parent = models.ForeignKey('Club', related_name='children', null=True, blank=True) parent = models.ForeignKey('Club', related_name='children', null=True, blank=True)
unix_name = models.CharField(_('unix name'), max_length=30, unique=True, unix_name = models.CharField(_('unix name'), max_length=30, unique=True,
@ -151,11 +152,21 @@ class Club(models.Model):
return False return False
return sub.is_subscribed return sub.is_subscribed
_memberships = {}
def get_membership_for(self, user): def get_membership_for(self, user):
""" """
Returns the current membership the given user Returns the current membership the given user
""" """
return self.members.filter(user=user.id).filter(end_date=None).first() try:
return Club._memberships[self.id][user.id]
except:
m = self.members.filter(user=user.id).filter(end_date=None).first()
try:
Club._memberships[self.id][user.id] = m
except:
Club._memberships[self.id] = {}
Club._memberships[self.id][user.id] = m
return m
class Membership(models.Model): class Membership(models.Model):
""" """