mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-01 04:10:06 -05:00
food to tree (model, api, serializer)
This commit is contained in:
57
cookbook/migrations/food_to_tree.py
Normal file
57
cookbook/migrations/food_to_tree.py
Normal file
@@ -0,0 +1,57 @@
|
||||
# Generated by Django 3.2.5 on 2021-08-14 15:40
|
||||
|
||||
from treebeard.mp_tree import MP_Node
|
||||
from django.db import migrations, models
|
||||
from django_scopes import scopes_disabled
|
||||
# update if needed
|
||||
steplen = MP_Node.steplen
|
||||
alphabet = MP_Node.alphabet
|
||||
node_order_by = ["name"]
|
||||
|
||||
|
||||
def update_paths(apps, schema_editor):
|
||||
with scopes_disabled():
|
||||
Node = apps.get_model("cookbook", "Food")
|
||||
nodes = Node.objects.all().order_by(*node_order_by)
|
||||
for i, node in enumerate(nodes, 1):
|
||||
# for default values, this resolves to: "{:04d}".format(i)
|
||||
node.path = f"{{:{alphabet[0]}{steplen}d}}".format(i)
|
||||
if nodes:
|
||||
Node.objects.bulk_update(nodes, ["path"])
|
||||
|
||||
|
||||
def backwards(apps, schema_editor):
|
||||
"""nothing to do"""
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('cookbook', '0147_auto_20210813_1829'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='food',
|
||||
name='depth',
|
||||
field=models.PositiveIntegerField(default=0),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='food',
|
||||
name='numchild',
|
||||
field=models.PositiveIntegerField(default=0),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='food',
|
||||
name='path',
|
||||
field=models.CharField(default=0, max_length=255, unique=False),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.RunPython(update_paths, backwards),
|
||||
migrations.AlterField(
|
||||
model_name="food",
|
||||
name="path",
|
||||
field=models.CharField(max_length=255, unique=True),
|
||||
),
|
||||
]
|
||||
Reference in New Issue
Block a user