from __future__ import unicode_literals

from django.db import migrations, models

TAGS = (
    # ( tag name, tag slug ),
    ("augmented reality", "augmented-reality"),
    ("big data", "big-data"),
    ("django", "django"),
    ("education", "education"),
    ("ipython", "ipython"),
    ("javascript", "javascript"),
    ("jupyter", "jupyter"),
    ("mobile", "mobile"),
    ("node.js", "node-js"),
    ("php", "php"),
    ("python", "python"),
    ("ruby on rails", "ruby-on-rails"),
    ("ruby", "ruby"),
    ("video games", "video-games"),
    ("web", "web"),
    ("zend", "zend"),
)


def add_tag_data(apps, schema_editor):
    Tag = apps.get_model('organizer', 'Tag')
    for tag_name, tag_slug in TAGS:
        Tag.objects.create(
            name=tag_name,
            slug=tag_slug)


def remove_tag_data(apps, schema_editor):
    Tag = apps.get_model('organizer', 'Tag')
    for _, tag_slug in TAGS:
        tag = Tag.objects.get(slug=tag_slug)
        tag.delete()


class Migration(migrations.Migration):

    dependencies = [
        ('organizer', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(
            add_tag_data,
            remove_tag_data)
    ]

from __future__ import unicode_literals

from django.db import migrations, models

TAGS = (
    # ( tag name, tag slug ),
    ("augmented reality", "augmented-reality"),
    ("big data", "big-data"),
    ("django", "django"),
    ("education", "education"),
    ("ipython", "ipython"),
    ("javascript", "javascript"),
    ("jupyter", "jupyter"),
    ("mobile", "mobile"),
    ("node.js", "node-js"),
    ("php", "php"),
    ("python", "python"),
    ("ruby on rails", "ruby-on-rails"),
    ("ruby", "ruby"),
    ("video games", "video-games"),
    ("web", "web"),
    ("zend", "zend"),
)


def add_tag_data(apps, schema_editor):
    Tag = apps.get_model('organizer', 'Tag')
    tag_list = []
    for tag_name, tag_slug in TAGS:
        tag_list.append(
            Tag(name=tag_name, slug=tag_slug))
    Tag.objects.bulk_create(tag_list)


def remove_tag_data(apps, schema_editor):
    Tag = apps.get_model('organizer', 'Tag')
    for _, tag_slug in TAGS:
        tag = Tag.objects.get(slug=tag_slug)
        tag.delete()


class Migration(migrations.Migration):

    dependencies = [
        ('organizer', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(
            add_tag_data,
            remove_tag_data)
    ]

Optimize addition of Tag data in migration.
Ch26: Optimize addition of Tag data in migration.

  from __future__ import unicode_literals
  
  from django.db import migrations, models
  
  TAGS = (
      # ( tag name, tag slug ),
      ("augmented reality", "augmented-reality"),
      ("big data", "big-data"),
      ("django", "django"),
      ("education", "education"),
      ("ipython", "ipython"),
      ("javascript", "javascript"),
      ("jupyter", "jupyter"),
      ("mobile", "mobile"),
      ("node.js", "node-js"),
      ("php", "php"),
      ("python", "python"),
      ("ruby on rails", "ruby-on-rails"),
      ("ruby", "ruby"),
      ("video games", "video-games"),
      ("web", "web"),
      ("zend", "zend"),
  )
  
  
  def add_tag_data(apps, schema_editor):
      Tag = apps.get_model('organizer', 'Tag')
+     tag_list = []
      for tag_name, tag_slug in TAGS:
-         Tag.objects.create(
-             name=tag_name,
-             slug=tag_slug)
+         tag_list.append(
+             Tag(name=tag_name, slug=tag_slug))
+     Tag.objects.bulk_create(tag_list)
  
  
  def remove_tag_data(apps, schema_editor):
      Tag = apps.get_model('organizer', 'Tag')
      for _, tag_slug in TAGS:
          tag = Tag.objects.get(slug=tag_slug)
          tag.delete()
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('organizer', '0001_initial'),
      ]
  
      operations = [
          migrations.RunPython(
              add_tag_data,
              remove_tag_data)
      ]
  
Optimize addition of Tag data in migration.
## Code Before:
from __future__ import unicode_literals

from django.db import migrations, models

TAGS = (
    # ( tag name, tag slug ),
    ("augmented reality", "augmented-reality"),
    ("big data", "big-data"),
    ("django", "django"),
    ("education", "education"),
    ("ipython", "ipython"),
    ("javascript", "javascript"),
    ("jupyter", "jupyter"),
    ("mobile", "mobile"),
    ("node.js", "node-js"),
    ("php", "php"),
    ("python", "python"),
    ("ruby on rails", "ruby-on-rails"),
    ("ruby", "ruby"),
    ("video games", "video-games"),
    ("web", "web"),
    ("zend", "zend"),
)


def add_tag_data(apps, schema_editor):
    Tag = apps.get_model('organizer', 'Tag')
    for tag_name, tag_slug in TAGS:
        Tag.objects.create(
            name=tag_name,
            slug=tag_slug)


def remove_tag_data(apps, schema_editor):
    Tag = apps.get_model('organizer', 'Tag')
    for _, tag_slug in TAGS:
        tag = Tag.objects.get(slug=tag_slug)
        tag.delete()


class Migration(migrations.Migration):

    dependencies = [
        ('organizer', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(
            add_tag_data,
            remove_tag_data)
    ]

## Instruction:
Optimize addition of Tag data in migration.
## Code After:
from __future__ import unicode_literals

from django.db import migrations, models

TAGS = (
    # ( tag name, tag slug ),
    ("augmented reality", "augmented-reality"),
    ("big data", "big-data"),
    ("django", "django"),
    ("education", "education"),
    ("ipython", "ipython"),
    ("javascript", "javascript"),
    ("jupyter", "jupyter"),
    ("mobile", "mobile"),
    ("node.js", "node-js"),
    ("php", "php"),
    ("python", "python"),
    ("ruby on rails", "ruby-on-rails"),
    ("ruby", "ruby"),
    ("video games", "video-games"),
    ("web", "web"),
    ("zend", "zend"),
)


def add_tag_data(apps, schema_editor):
    Tag = apps.get_model('organizer', 'Tag')
    tag_list = []
    for tag_name, tag_slug in TAGS:
        tag_list.append(
            Tag(name=tag_name, slug=tag_slug))
    Tag.objects.bulk_create(tag_list)


def remove_tag_data(apps, schema_editor):
    Tag = apps.get_model('organizer', 'Tag')
    for _, tag_slug in TAGS:
        tag = Tag.objects.get(slug=tag_slug)
        tag.delete()


class Migration(migrations.Migration):

    dependencies = [
        ('organizer', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(
            add_tag_data,
            remove_tag_data)
    ]
