Created
June 29, 2022 21:07
-
-
Save aliceridgway/b69eea69aab407dbb2ab49a6f194d428 to your computer and use it in GitHub Desktop.
A custom migration that creates a category to be used as the default category for a post.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generated by Django 4.0.5 on 2022-06-29 20:33 | |
from django.db import migrations | |
DEFAULT_CATEGORY = "Uncategorised" | |
def add_default_category(apps, _): | |
category_model = apps.get_model(app_label="blog", model_name="Category") | |
category_model.objects.get_or_create(name=DEFAULT_CATEGORY) | |
def remove_default_category(apps, _): | |
category_model = apps.get_model(app_label="blog", model_name="Category") | |
category = category_model.objects.filter(name=DEFAULT_CATEGORY).first() | |
if category: | |
category.delete() | |
class Migration(migrations.Migration): | |
dependencies = [ | |
("blog", "0006_category_post_category"), | |
] | |
operations = [migrations.RunPython(add_default_category, remove_default_category)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment