62 lines
2 KiB
Python
62 lines
2 KiB
Python
# Generated by Django 3.1.5 on 2021-03-03 15:33
|
|
|
|
from django.db import migrations, models
|
|
|
|
|
|
def forwards(apps, schema_editor):
|
|
Tag = apps.get_model("articles", "Tag")
|
|
Article = apps.get_model("articles", "Article")
|
|
db_alias = schema_editor.connection.alias
|
|
articles = Article.objects.using(db_alias).all()
|
|
for article in articles:
|
|
tags = []
|
|
for keyword in list(
|
|
filter(None, map(lambda k: k.strip(), article.keywords.split(",")))
|
|
):
|
|
tag = Tag.objects.using(db_alias).filter(name__iexact=keyword).first()
|
|
if tag is None:
|
|
tag = Tag.objects.create(name=keyword)
|
|
tags.append(tag)
|
|
article.tags.set(tags)
|
|
article.keywords = ""
|
|
Article.objects.bulk_update(articles, ["keywords"])
|
|
|
|
|
|
def backwards(apps, schema_editor):
|
|
Article = apps.get_model("articles", "Article")
|
|
db_alias = schema_editor.connection.alias
|
|
articles = Article.objects.using(db_alias).all()
|
|
for article in articles:
|
|
article.keywords = ",".join(map(lambda tag: tag.name, article.tags.all()))
|
|
Article.objects.bulk_update(articles, ["keywords"])
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
("articles", "0026_article_draft_key"),
|
|
]
|
|
|
|
operations = [
|
|
migrations.CreateModel(
|
|
name="Tag",
|
|
fields=[
|
|
(
|
|
"id",
|
|
models.AutoField(
|
|
auto_created=True,
|
|
primary_key=True,
|
|
serialize=False,
|
|
verbose_name="ID",
|
|
),
|
|
),
|
|
("name", models.CharField(max_length=255, unique=True)),
|
|
],
|
|
),
|
|
migrations.AddField(
|
|
model_name="article",
|
|
name="tags",
|
|
field=models.ManyToManyField(related_name="articles", to="articles.Tag"),
|
|
),
|
|
migrations.RunPython(forwards, backwards),
|
|
]
|