Make a few database performance optimisations
This commit is contained in:
parent
eaf54a4e83
commit
824aaa1e57
4 changed files with 61 additions and 8 deletions
27
entries/migrations/0004_auto_20171027_0846.py
Normal file
27
entries/migrations/0004_auto_20171027_0846.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.6 on 2017-10-26 21:46
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('entries', '0003_remove_entry_summary'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='entry',
|
||||
name='author',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='entries', to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='entry',
|
||||
name='kind',
|
||||
field=models.CharField(choices=[('note', 'note'), ('article', 'article')], db_index=True, default='note', max_length=30),
|
||||
),
|
||||
]
|
|
@ -10,17 +10,29 @@ from . import kinds
|
|||
ENTRY_KINDS = [(k.id, k.id) for k in kinds.all]
|
||||
|
||||
|
||||
class EntryManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
qs = super(EntryManager, self).get_queryset()
|
||||
return qs.select_related('author').prefetch_related('syndications')
|
||||
|
||||
|
||||
class Entry(ModelMeta, models.Model):
|
||||
objects = EntryManager()
|
||||
kind = models.CharField(
|
||||
max_length=30,
|
||||
choices=ENTRY_KINDS,
|
||||
db_index=True,
|
||||
default=ENTRY_KINDS[0][0]
|
||||
)
|
||||
|
||||
name = models.CharField(max_length=100, blank=True)
|
||||
content = models.TextField()
|
||||
|
||||
author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
|
||||
author = models.ForeignKey(
|
||||
get_user_model(),
|
||||
related_name='entries',
|
||||
on_delete=models.CASCADE,
|
||||
)
|
||||
|
||||
published = models.DateTimeField()
|
||||
updated = models.DateTimeField()
|
||||
|
@ -70,7 +82,14 @@ class Entry(ModelMeta, models.Model):
|
|||
ordering = ['-published']
|
||||
|
||||
|
||||
class SyndicationManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
qs = super(SyndicationManager, self).get_queryset()
|
||||
return qs.select_related('profile__site')
|
||||
|
||||
|
||||
class Syndication(models.Model):
|
||||
objects = SyndicationManager()
|
||||
entry = models.ForeignKey(
|
||||
Entry,
|
||||
related_name='syndications',
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
from django.shortcuts import get_object_or_404, render
|
||||
from users.models import User
|
||||
from entries.models import Entry
|
||||
from lemoncurry import breadcrumbs
|
||||
|
||||
breadcrumbs.add('home:index', 'home')
|
||||
|
||||
|
||||
def index(request):
|
||||
user = get_object_or_404(User, pk=1)
|
||||
entries = Entry.objects.filter(author=user)
|
||||
query = User.objects.prefetch_related('entries', 'profiles', 'keys')
|
||||
user = get_object_or_404(query, pk=1)
|
||||
entries = user.entries.all()
|
||||
return render(request, 'home/index.html', {
|
||||
'user': user,
|
||||
'entries': entries,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from django.db import models
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.utils.functional import cached_property
|
||||
from meta.models import ModelMeta
|
||||
|
||||
|
||||
|
@ -30,17 +31,17 @@ class User(ModelMeta, AbstractUser):
|
|||
def avatar_url(self):
|
||||
return self.avatar.url
|
||||
|
||||
@property
|
||||
@cached_property
|
||||
def facebook_id(self):
|
||||
try:
|
||||
return self.profiles.filter(site__name='Facebook')[0].username
|
||||
return self.profiles.filter(site__name='Facebook').values('username')[0]['username']
|
||||
except IndexError:
|
||||
return None
|
||||
|
||||
@property
|
||||
@cached_property
|
||||
def twitter_username(self):
|
||||
try:
|
||||
return '@' + self.profiles.filter(site__name='Twitter')[0].username
|
||||
return '@' + self.profiles.filter(site__name='Twitter').values('username')[0]['username']
|
||||
except IndexError:
|
||||
return None
|
||||
|
||||
|
@ -53,7 +54,13 @@ class User(ModelMeta, AbstractUser):
|
|||
}
|
||||
|
||||
|
||||
class ProfileManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return super(ProfileManager, self).get_queryset().select_related('site')
|
||||
|
||||
|
||||
class Profile(models.Model):
|
||||
objects = ProfileManager()
|
||||
user = models.ForeignKey(
|
||||
User,
|
||||
related_name='profiles',
|
||||
|
|
Loading…
Reference in a new issue