from __future__ import unicode_literals


from django.db import migrations
import mimetypes


def forward(apps, schema_editor):
    AttachmentModel = apps.get_model('common', 'Attachment')
    for attachment in AttachmentModel.objects.all():
        if mimetypes.guess_type(attachment.attachment_file.name, strict=True)[0].split('/')[0].startswith('image'):
            attachment.is_image = True
            attachment.save()


class Migration(migrations.Migration):

    dependencies = [
        ('common', '0010_attachment_is_image'),
    ]

    operations = [
        migrations.RunPython(forward),
    ]

from __future__ import unicode_literals


from django.db import migrations
import mimetypes


def forward(apps, schema_editor):
    AttachmentModel = apps.get_model('common', 'Attachment')
    for attachment in AttachmentModel.objects.all():
        mt = mimetypes.guess_type(attachment.attachment_file.name, strict=True)[0]
        if mt is not None and mt.split('/')[0].startswith('image'):
            attachment.is_image = True
            attachment.save()


class Migration(migrations.Migration):

    dependencies = [
        ('common', '0010_attachment_is_image'),
    ]

    operations = [
        migrations.RunPython(forward),
    ]

Check if mt is None before spliting it
Check if mt is None before spliting it

  from __future__ import unicode_literals
  
  
  from django.db import migrations
  import mimetypes
  
  
  def forward(apps, schema_editor):
      AttachmentModel = apps.get_model('common', 'Attachment')
      for attachment in AttachmentModel.objects.all():
-         if mimetypes.guess_type(attachment.attachment_file.name, strict=True)[0].split('/')[0].startswith('image'):
+         mt = mimetypes.guess_type(attachment.attachment_file.name, strict=True)[0]
+         if mt is not None and mt.split('/')[0].startswith('image'):
              attachment.is_image = True
              attachment.save()
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('common', '0010_attachment_is_image'),
      ]
  
      operations = [
          migrations.RunPython(forward),
      ]
  
Check if mt is None before spliting it
## Code Before:
from __future__ import unicode_literals


from django.db import migrations
import mimetypes


def forward(apps, schema_editor):
    AttachmentModel = apps.get_model('common', 'Attachment')
    for attachment in AttachmentModel.objects.all():
        if mimetypes.guess_type(attachment.attachment_file.name, strict=True)[0].split('/')[0].startswith('image'):
            attachment.is_image = True
            attachment.save()


class Migration(migrations.Migration):

    dependencies = [
        ('common', '0010_attachment_is_image'),
    ]

    operations = [
        migrations.RunPython(forward),
    ]

## Instruction:
Check if mt is None before spliting it
## Code After:
from __future__ import unicode_literals


from django.db import migrations
import mimetypes


def forward(apps, schema_editor):
    AttachmentModel = apps.get_model('common', 'Attachment')
    for attachment in AttachmentModel.objects.all():
        mt = mimetypes.guess_type(attachment.attachment_file.name, strict=True)[0]
        if mt is not None and mt.split('/')[0].startswith('image'):
            attachment.is_image = True
            attachment.save()


class Migration(migrations.Migration):

    dependencies = [
        ('common', '0010_attachment_is_image'),
    ]

    operations = [
        migrations.RunPython(forward),
    ]
