db va type togirlandi
This commit is contained in:
43
core/apps/api/migrations/0009_typemodel_category_type_fk.py
Normal file
43
core/apps/api/migrations/0009_typemodel_category_type_fk.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
"""
|
||||
1-qadam: TypeModel jadvalini yaratish va categoryga type_fk ustunini qo'shish.
|
||||
Eski 'type' (CharField) ustuni hali o'chmaydi — ma'lumot saqlanadi.
|
||||
"""
|
||||
|
||||
dependencies = [
|
||||
('api', '0008_subproductmodel_description'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='TypeModel',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('name', models.CharField(max_length=255, verbose_name='name')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'TypeModel',
|
||||
'verbose_name_plural': 'TypeModels',
|
||||
'db_table': 'type',
|
||||
},
|
||||
),
|
||||
# Yangi FK ustun qo'shamiz (type_fk_id), eski type ustuni saqlanib qoladi
|
||||
migrations.AddField(
|
||||
model_name='categorymodel',
|
||||
name='type_fk',
|
||||
field=models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_NULL,
|
||||
related_name='categories',
|
||||
to='api.typemodel',
|
||||
verbose_name='type',
|
||||
),
|
||||
),
|
||||
]
|
||||
48
core/apps/api/migrations/0010_migrate_type_data.py
Normal file
48
core/apps/api/migrations/0010_migrate_type_data.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def forwards(apps, schema_editor):
|
||||
"""
|
||||
Eski type (CharField: RESTAURANT/BAR) qiymatlaridan TypeModel yozuvlari yaratadi,
|
||||
keyin har bir categoryni to'g'ri TypeModelga ulaydi.
|
||||
"""
|
||||
TypeModel = apps.get_model('api', 'TypeModel')
|
||||
CategoryModel = apps.get_model('api', 'CategoryModel')
|
||||
|
||||
# DB dagi barcha unique type qiymatlarini topib TypeModel yozuvlari yaratamiz
|
||||
existing_types = (
|
||||
CategoryModel.objects.exclude(type__isnull=True)
|
||||
.exclude(type='')
|
||||
.values_list('type', flat=True)
|
||||
.distinct()
|
||||
)
|
||||
|
||||
type_map = {}
|
||||
for type_value in existing_types:
|
||||
obj, _ = TypeModel.objects.get_or_create(name=type_value)
|
||||
type_map[type_value] = obj
|
||||
|
||||
# Har bir categoryni mos TypeModelga ulaymiz
|
||||
for category in CategoryModel.objects.exclude(type__isnull=True).exclude(type=''):
|
||||
category.type_fk = type_map.get(category.type)
|
||||
category.save(update_fields=['type_fk'])
|
||||
|
||||
|
||||
def backwards(apps, schema_editor):
|
||||
CategoryModel = apps.get_model('api', 'CategoryModel')
|
||||
CategoryModel.objects.update(type_fk=None)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
"""
|
||||
2-qadam: Mavjud type (RESTAURANT/BAR) qiymatlaridan TypeModel yozuvlari yaratib,
|
||||
categorylarni ulaydi.
|
||||
"""
|
||||
|
||||
dependencies = [
|
||||
('api', '0009_typemodel_category_type_fk'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(forwards, backwards),
|
||||
]
|
||||
26
core/apps/api/migrations/0011_category_type_cleanup.py
Normal file
26
core/apps/api/migrations/0011_category_type_cleanup.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
"""
|
||||
3-qadam: Eski type (CharField) ustunini o'chirib, type_fk ni type deb nomini o'zgartirish.
|
||||
"""
|
||||
|
||||
dependencies = [
|
||||
('api', '0010_migrate_type_data'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
# Eski CharField type ustunini o'chiramiz
|
||||
migrations.RemoveField(
|
||||
model_name='categorymodel',
|
||||
name='type',
|
||||
),
|
||||
# type_fk ni type deb nomini o'zgartirамiz
|
||||
migrations.RenameField(
|
||||
model_name='categorymodel',
|
||||
old_name='type_fk',
|
||||
new_name='type',
|
||||
),
|
||||
]
|
||||
Reference in New Issue
Block a user