Added initial migration for catalogue app.
[wolnelektury.git] / migrations / 001_add_book_count_to_shelf.py
1 from django.core.management import setup_environ
2 import settings
3
4 setup_environ(settings)
5
6 from catalogue.models import Tag, Book
7 from django.db import connection
8
9 query = 'ALTER TABLE catalogue_tag ADD COLUMN book_count integer NOT NULL DEFAULT 0'
10
11 cursor = connection.cursor()
12 cursor.execute(query)
13
14 for shelf in Tag.objects.filter(category='set'):
15     shelf.book_count = len(Book.tagged.with_all(shelf))
16     shelf.save()