From 4fed56630d8e4eed11e1289da0077f7187d8c687 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 26 Jul 2018 21:48:30 +0100 Subject: [PATCH] Allow tagging blog posts --- .../migrations/0002_auto_20180726_2038.py | 33 +++++++++++++++++++ project/blog/models.py | 20 +++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 project/blog/migrations/0002_auto_20180726_2038.py diff --git a/project/blog/migrations/0002_auto_20180726_2038.py b/project/blog/migrations/0002_auto_20180726_2038.py new file mode 100644 index 0000000..4b4e6e3 --- /dev/null +++ b/project/blog/migrations/0002_auto_20180726_2038.py @@ -0,0 +1,33 @@ +# Generated by Django 2.0.7 on 2018-07-26 20:38 + +from django.db import migrations, models +import django.db.models.deletion +import modelcluster.contrib.taggit +import modelcluster.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('taggit', '0002_auto_20150616_2121'), + ('blog', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='BlogPageTag', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('content_object', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='tagged_items', to='blog.BlogPage')), + ('tag', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='blog_blogpagetag_items', to='taggit.Tag')), + ], + options={ + 'abstract': False, + }, + ), + migrations.AddField( + model_name='blogpage', + name='tags', + field=modelcluster.contrib.taggit.ClusterTaggableManager(blank=True, help_text='A comma-separated list of tags.', through='blog.BlogPageTag', to='taggit.Tag', verbose_name='Tags'), + ), + ] diff --git a/project/blog/models.py b/project/blog/models.py index da37c85..8f48de5 100644 --- a/project/blog/models.py +++ b/project/blog/models.py @@ -1,16 +1,32 @@ from project.common.blocks import build_stream_field -from wagtail.admin.edit_handlers import StreamFieldPanel +from wagtail.admin.edit_handlers import StreamFieldPanel, FieldPanel from wagtail.search import index from project.common.models import Entity +from django.db import models + +from modelcluster.fields import ParentalKey +from modelcluster.contrib.taggit import ClusterTaggableManager +from taggit.models import TaggedItemBase + + +class BlogPageTag(TaggedItemBase): + content_object = ParentalKey( + 'BlogPage', + related_name='tagged_items', + on_delete=models.CASCADE + ) class BlogPage(Entity): body = build_stream_field() + tags = ClusterTaggableManager(through=BlogPageTag, blank=True) + search_fields = Entity.search_fields + [ index.SearchField('body'), ] content_panels = Entity.content_panels + [ StreamFieldPanel('body'), -] + FieldPanel('tags') + ]