Using the contrib.categories module¶
Flexible category system for organizing content using the relate_to configuration.
Installation¶
INSTALLED_APPS = [
"djangocms_custom_content",
"djangocms_custom_content.contrib.categories",
]
python manage.py migrate
Models¶
FlatCategory - A simple category model (inherits from AbstractCustomContent)
Fields: title, slug, is_featured
The FlatCategory model uses the relate_to configuration to automatically add a categories accessor to BlogPost:
class CMSConfig:
relate_to = [("categories", "djangocms_custom_content_blog.BlogPost")]
Usage¶
Create a category:
from djangocms_custom_content.contrib.categories.models import FlatCategory
category = FlatCategory.objects.create(
title="Technology",
slug="technology",
is_featured=True,
)
Link categories to blog posts:
from djangocms_custom_content.contrib.blog.models import BlogPost
blog_post = BlogPost.objects.first()
# Add a category
blog_post.categories.add(category)
# Get all categories
categories = blog_post.categories.all()
# Remove a category
blog_post.categories.remove(category)
# Clear all categories
blog_post.categories.clear()
Plugins¶
FlatCategoryList- Display all categories
Admin¶
FlatCategory is registered with search by title and slug.
In Templates¶
<!-- Display categories linked to a blog post -->
{% for category in blog_post.categories.all %}
<a href="#" class="category" data-featured="{{ category.is_featured }}">
{{ category.title }}
</a>
{% endfor %}
<!-- Filter featured categories only -->
{% for category in blog_post.categories.all %}
{% if category.is_featured %}
<span class="featured-category">{{ category.title }}</span>
{% endif %}
{% endfor %}
See Also¶
Set Up Many-to-Many Relations - Learn about
relate_toandinvite_m2m_relationsM2M Relationships Explained - How M2M relations work