Reference¶
API reference automatically generated from source code.
CMSConfig options¶
Declare a CMSConfig inner class on a content model to opt into CMS
integration. All options are read at app startup and default to off.
Option |
Default |
Effect |
|---|---|---|
|
|
Register the content model with djangocms-versioning (which must be
installed). If the model has a |
|
|
Make the content frontend-editable (double-click editing via the CMS
toolbar). The grouper admin should mix in |
|
|
Register a CMS app hook exposing a detail view. The URL uses the model’s
|
|
|
Add a shortcut to the grouper changelist in the toolbar’s admin (site) menu. Entries are sorted alphabetically above the Administration break and are only shown to users with view permission on the grouper. |
Whether a content model participates as a grouper/content pair is inferred from
its foreign key to an AbstractCustomGrouper;
a content model without such a foreign key is treated as a plain model (no
versioning, apphook or grouper admin).
Related setting:
CMS_SETTINGS_SHORTCUT(defaultTrue) — show the settings-cog button for the current grouper in the toolbar.
Models¶
- class djangocms_custom_content.models.AbstractCustomGrouper(*args, **kwargs)¶
Bases:
CustomGrouperMixin,ModelAbstract base model for grouper objects.
A grouper is a container that organizes multiple language versions of content. Inherit from this model when you want to group content versions together.
The grouper automatically discovers its related content model and provides methods to access content by language.
Example:
class Article(AbstractCustomGrouper): '''Groups all language versions of an article.''' pass class ArticleContent(AbstractCustomContent): article = ForeignKey(Article, on_delete=CASCADE) title = CharField(max_length=200) language = CharField(max_length=5)
- Attributes:
_content_set: Cache for the related content model manager _has_language_field: Whether the content model has a language field _content_cache: Cache for retrieved content instances _is_admin_cache: Flag for admin-specific caching
- get_content(language=None)¶
Retrieve content for this grouper in a specific language.
Uses the current language by default if no language is specified. Implements caching for performance.
- Args:
language: The language code to retrieve. Defaults to current language.- Returns:
The content model instance for the specified language, or None if not found
Example:
article = Article.objects.first() english_content = article.get_content(language='en') german_content = article.get_content(language='de')
- Parameters:
language (str | None)
- Return type:
Model | None
- get_admin_content(language=None)¶
Retrieve content for admin interface with prefetch optimization.
This method is optimized for admin display and uses prefetched data when available. It retrieves the latest published content.
- Args:
language: The language code to retrieve. Defaults to current language.- Returns:
The latest content model instance for the specified language, or None if not found
- Notes:
This method is primarily used by the Django admin interface.
- Parameters:
language (str | None)
- Return type:
Model | None
- class djangocms_custom_content.models.AbstractCustomContent(*args, **kwargs)¶
Bases:
CustomContentMixin,ModelAbstract base model providing a PlaceholderRelationField for custom content.
Inherit from this model in your project to quickly add placeholder support to your custom content types.
To relate content groupers to one another, declare a
RelationFieldon the grouper model. Seedjangocms_custom_content.relations.- objects¶
- admin_manager¶
- placeholders¶
Accessor to the related objects manager on the one-to-many relation created by GenericRelation.
In the example:
class Post(Model): comments = GenericRelation(Comment)
post.commentsis a ReverseGenericManyToOneDescriptor instance.
- template_name_suffix = '_detail'¶
- class djangocms_custom_content.models.CustomGrouperMixin¶
Bases:
objectMixin providing base grouper functionality.
This mixin is inherited by
AbstractCustomGrouperto provide grouper model functionality. It serves as a marker class for identifying grouper models in the framework.
- class djangocms_custom_content.models.CustomContentMixin¶
Bases:
objectMixin providing base content model functionality.
This mixin is inherited by
AbstractCustomContentto provide content model functionality. It serves as a marker class for identifying content models in the framework.
Relations¶
- class djangocms_custom_content.relations.RelationField(target, *, related_name=None, ordered=False, through_name=None)¶
Bases:
objectDeclarative grouper-to-grouper relation, à la
ManyToManyField.targetmay be a model class or an"app_label.Model"string naming either a grouper or its content model (resolved to the grouper). The forward accessor is installed immediately; the reverse accessor named byrelated_nameis installed on the target once the app registry is ready.- contribute_to_class(cls, name)¶
- class djangocms_custom_content.relations.RelationManager(instance, through, target_model, *, ordered)¶
Bases:
objectForward accessor
owner.<field>→ a queryset of target groupers.throughrows are filtered bysource=owner; the target groupers are returned as a genuine queryset so all queryset methods are available. When the through is ordered, results are ordered by the through’sorder.- Parameters:
- get_queryset()¶
- Return type:
QuerySet
- all()¶
- Return type:
QuerySet
- add(*objs)¶
- Parameters:
objs (Model)
- Return type:
None
- remove(*objs)¶
- Parameters:
objs (Model)
- Return type:
None
- clear()¶
- Return type:
None
- class djangocms_custom_content.relations.ReverseRelationManager(instance, through, source_model)¶
Bases:
objectReverse accessor
target.<related_name>→ queryset of source groupers.- get_queryset()¶
- Return type:
QuerySet
- all()¶
- Return type:
QuerySet
- add(*objs)¶
- Parameters:
objs (Model)
- Return type:
None
- remove(*objs)¶
- Parameters:
objs (Model)
- Return type:
None
- clear()¶
- Return type:
None
- djangocms_custom_content.relations.relation_through_factory(owner_model, *, ordered=False, name=None, module=None)¶
Build a concrete through model linking
owner_modelto any grouper.The returned model has a concrete
sourceFK toowner_model, the inherited generictarget, a uniqueness constraint preventing duplicate edges and an index on the generic columns for fast reverse lookups.
- djangocms_custom_content.relations.grouper_model_of(model)¶
Return the grouper model for a content model, else
modelunchanged.Relations always target the grouper so they survive version copies. A declaration may name either the grouper or its content model; both resolve here to the grouper (“if there is one”).
CMS Toolbars¶
- class djangocms_custom_content.cms_toolbars.CustomContentToolbar(request, toolbar, is_current_app, app_path)¶
Bases:
CMSToolbarToolbar for djangocms-custom-content.
Adds a settings icon button to the right toolbar.
- classmethod get_insert_position(admin_menu, item_name)¶
Return the position for a custom content shortcut in the admin (site) menu.
Entries are grouped directly above the
ADMINISTRATION_BREAKand ordered alphabetically among themselves, following the convention used by djangocms-stories and djangocms-alias. The first menu item (the current site entry) is always kept in place.Note that django CMS core adds the
SHORTCUTS_BREAKbelow theADMINISTRATION_BREAK(near the “Logout” entry), so it cannot be used as an upper anchor for the shortcut region – theADMINISTRATION_BREAKis the only reliable boundary.- Args:
admin_menu: The CMS admin menu instance to inspect and modify.item_name: The menu item name used for alphabetical ordering.- Returns:
The integer position where the item should be inserted.
- add_shortcut_links()¶
Add admin-menu shortcut links for content that opted in.
A model is included when its content sets
admin_menu = Trueon itsCMSConfig. The linked changelist is the grouper’s for grouper-backed content, or the content model’s own for plain (grouper-less) content such asFlatCategory(seeadmin_menu_modelsin the app config).
Add menu entries for the current grouper instance.
- populate()¶
Populate the toolbar with custom content entries.
- property media¶
Admin¶
- class djangocms_custom_content.admin.CustomGrouperAdminMixin¶
Bases:
RelationAdminMixinAdmin mixin to redirect content endpoints for grouper admins.
Provides a breadcrumb redirect compatible with django CMS versioning, and (via
RelationAdminMixin) renders anyRelationFieldon the model as an autocomplete multi-select — sortable when the relation is ordered.Prefetching of the latest related content (
_admin_prefetch_cache) is handled bycms.admin.utils.GrouperModelAdmin.get_queryset, which the concrete admin classes inherit from, so this mixin does not overrideget_queryset.- get_urls()¶
Register breadcrumb redirect URLs for grouper admin views.
- breadcrumb_redir(request, *args, **kwargs)¶
Redirect versioning breadcrumb URLs to the grouper admin.
djangocms-versioning uses content admin URLs for breadcrumbs, but this project uses grouper admin classes and must redirect accordingly.
- class djangocms_custom_content.relation_admin.RelationAdminMixin¶
Bases:
objectRenders the model’s relation fields (declared by
RelationModelForm) with autocomplete widgets — sortable when the relation is ordered.Field creation, initial values and saving live in
RelationModelForm; this mixin only ensures that form is in use, swaps in the autocomplete widgets, and serves the autocomplete endpoint. Setrelation_autocomplete = Falseto opt out.- relation_autocomplete = True¶
- get_urls()¶
- relation_autocomplete_view(request)¶
- get_form(request, obj=None, **kwargs)¶
Helpers¶
- djangocms_custom_content.helpers.get_custom_config(model)¶
Return the custom content configuration tuple for the given model.
Looks up the model in the
custom_content_groupersregistry and returns a tuple of(content_model, grouper_field_name, enable_versioning). If the model is not registered, returns(None, None, False).