1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
4 from django.db import migrations, models
7 def refresh_books(apps, schema_editor):
8 Book = apps.get_model('catalogue', 'Book')
9 TagRelation = apps.get_model('catalogue', 'TagRelation')
10 db_alias = schema_editor.connection.alias
11 for book in Book.objects.using(db_alias).all():
12 book.cached_author = ', '.join(
13 TagRelation.objects.filter(content_type__model='book', object_id=book.id, tag__category='author')
14 .values_list('tag__name', flat=True))
15 book.has_audience = 'audience' in book.extra_info
19 class Migration(migrations.Migration):
22 ('catalogue', '0015_book_recommended'),
29 field=models.CharField(db_index=True, max_length=240, blank=True),
34 field=models.BooleanField(default=False),
36 migrations.RunPython(refresh_books, lambda apps, schema_editor: None),