<filename>property/migrations/0009_auto_20210209_1430.py
# Generated by Django 2.2.4 on 2021-02-09 11:30
import phonenumbers
from django.db import migrations


def phonenumber_format(apps, schema_editor):
    Flat = apps.get_model('property', 'Flat')
    for flat in Flat.objects.all():
        phone = flat.owners_phonenumber
        parsed_phone = phonenumbers.parse(phone, 'RU')
        if phonenumbers.is_valid_number(parsed_phone):
            flat.owner_pure_phone = phonenumbers.format_number(parsed_phone, phonenumbers.PhoneNumberFormat.E164)
            flat.save()
        else:
            flat.owner_pure_phone = ''
            flat.save()


class Migration(migrations.Migration):

    dependencies = [
        ('property', '0008_flat_owner_pure_phone'),
    ]

    operations = [
        migrations.RunPython(phonenumber_format)
    ]

<filename>property/migrations/0009_auto_20210209_1430.py
# Generated by Django 2.2.4 on 2021-02-09 11:30
import phonenumbers
from django.db import migrations


def phonenumber_format(apps, schema_editor):
    Flat = apps.get_model('property', 'Flat')
    for flat in Flat.objects.all():
        phone = flat.owners_phonenumber
        parsed_phone = phonenumbers.parse(phone, 'RU')
        if phonenumbers.is_valid_number(parsed_phone):
            flat.owner_pure_phone = phonenumbers.format_number(parsed_phone, phonenumbers.PhoneNumberFormat.E164)
            flat.save()
        else:
            flat.owner_pure_phone = ''
            flat.save()


class Migration(migrations.Migration):

    dependencies = [
        ('property', '0008_flat_owner_pure_phone'),
    ]

    operations = [
        migrations.RunPython(phonenumber_format)
    ]

# Generated by Django 2.2.4 on 2021-02-09 11:30