from django.db import migrations
from bluebottle.clients import properties


def set_default(apps, schema_editor):
    try: 
        Language = apps.get_model('utils', 'Language')

        language = Language.objects.get(code=properties.LANGUAGE_CODE)
    except Language.DoesNotExist:
        language = Language.objects.first()

    language.default = True
    language.save()


class Migration(migrations.Migration):

    dependencies = [
        ('utils', '0006_auto_20210825_1018'),
    ]

    operations = [
        migrations.RunPython(set_default, migrations.RunPython.noop)
    ]

from django.db import migrations
from bluebottle.clients import properties


def set_default(apps, schema_editor):
    try: 
        Language = apps.get_model('utils', 'Language')

        language = Language.objects.get(code=properties.LANGUAGE_CODE)
    except Language.DoesNotExist:
        try:
            language = Language.objects.get()
        except Language.DoesNotExist:
            return

    language.default = True
    language.save()


class Migration(migrations.Migration):

    dependencies = [
        ('utils', '0006_auto_20210825_1018'),
    ]

    operations = [
        migrations.RunPython(set_default, migrations.RunPython.noop)
    ]

Fix migration if no language exists
Fix migration if no language exists

  from django.db import migrations
  from bluebottle.clients import properties
  
  
  def set_default(apps, schema_editor):
      try: 
          Language = apps.get_model('utils', 'Language')
  
          language = Language.objects.get(code=properties.LANGUAGE_CODE)
      except Language.DoesNotExist:
+         try:
-         language = Language.objects.first()
+             language = Language.objects.get()
+         except Language.DoesNotExist:
+             return
  
      language.default = True
      language.save()
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('utils', '0006_auto_20210825_1018'),
      ]
  
      operations = [
          migrations.RunPython(set_default, migrations.RunPython.noop)
      ]
  
Fix migration if no language exists
## Code Before:
from django.db import migrations
from bluebottle.clients import properties


def set_default(apps, schema_editor):
    try: 
        Language = apps.get_model('utils', 'Language')

        language = Language.objects.get(code=properties.LANGUAGE_CODE)
    except Language.DoesNotExist:
        language = Language.objects.first()

    language.default = True
    language.save()


class Migration(migrations.Migration):

    dependencies = [
        ('utils', '0006_auto_20210825_1018'),
    ]

    operations = [
        migrations.RunPython(set_default, migrations.RunPython.noop)
    ]

## Instruction:
Fix migration if no language exists
## Code After:
from django.db import migrations
from bluebottle.clients import properties


def set_default(apps, schema_editor):
    try: 
        Language = apps.get_model('utils', 'Language')

        language = Language.objects.get(code=properties.LANGUAGE_CODE)
    except Language.DoesNotExist:
        try:
            language = Language.objects.get()
        except Language.DoesNotExist:
            return

    language.default = True
    language.save()


class Migration(migrations.Migration):

    dependencies = [
        ('utils', '0006_auto_20210825_1018'),
    ]

    operations = [
        migrations.RunPython(set_default, migrations.RunPython.noop)
    ]
