Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aliceridgway/b69eea69aab407dbb2ab49a6f194d428 to your computer and use it in GitHub Desktop.
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.
# 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