From 779ee06149d8eb542555ae1233e51f05e80bb1de Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 10 Jan 2024 21:59:54 +0000 Subject: [PATCH] Support many-to-many with tags --- content/core.page/test2.md | 2 ++ content/core.tag/tags.yml | 2 ++ requirements.txt | 2 +- yamdl_playground/core/models.py | 22 +++++++++++++++++++++- 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 content/core.tag/tags.yml diff --git a/content/core.page/test2.md b/content/core.page/test2.md index 3ba7a60..f408913 100644 --- a/content/core.page/test2.md +++ b/content/core.page/test2.md @@ -1,5 +1,7 @@ --- title: Test +tags: + - tag-1 --- # Heading diff --git a/content/core.tag/tags.yml b/content/core.tag/tags.yml new file mode 100644 index 0000000..a88c53d --- /dev/null +++ b/content/core.tag/tags.yml @@ -0,0 +1,2 @@ +- slug: tag-1 +- slug: tag-2 diff --git a/requirements.txt b/requirements.txt index d9fbf0c..1d2435d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ django -git+https://github.com/andrewgodwin/yamdl +git+https://github.com/andrewgodwin/yamdl@8d34d7397f3564a550042e81ea79cca7d2f3777e gunicorn markdown diff --git a/yamdl_playground/core/models.py b/yamdl_playground/core/models.py index 3d0314d..1c951e4 100644 --- a/yamdl_playground/core/models.py +++ b/yamdl_playground/core/models.py @@ -1,9 +1,29 @@ from django.db import models +class Tag(models.Model): + __yamdl__ = True + + slug = models.SlugField(max_length=128, primary_key=True) + + content = models.TextField() + + class Page(models.Model): __yamdl__ = True title = models.CharField(max_length=255) - slug = models.CharField(max_length=128, unique=True, db_index=True, default=None, null=True) content = models.TextField() + slug = models.CharField(max_length=128, unique=True, db_index=True, default=None, null=True) + + tags = models.ManyToManyField(Tag) + + @classmethod + def from_yaml(cls, **data): + tags = data.pop("tags", None) + instance = cls.objects.create(**data) + + if tags: + instance.tags.set(tags) + + return instance