🗂️ Displaying content
Editors find and manage content on the list page, and get an overview on the dashboard. This guide sets up the article's list page: its columns and formats, search and filters, the views beyond the table, editing in place, actions and export, and then puts articles on the dashboard. Every option is on List page and Dashboard.
It uses the Article model from the forms guide,
with a views count added:
# blog/models.py (a fragment)
views = models.PositiveIntegerField(default=0)
1. Choose the columns
table_display lists the table's columns. Each column shows its value
through a format that fits the field's type: a
date in the editor's language, a related object, a thumbnail. Pick another
format, or a header, with a TableColumn:
# blog/admin.py
from content_studio import register
from content_studio.admin import ModelStudio
from content_studio.form import TableColumn
from content_studio.formats import BadgeFormat, NumberFormat
from .models import Article
@register(Article)
class ArticleStudio(ModelStudio):
table_display = [
"title",
"category",
TableColumn(
field="status",
format=BadgeFormat(colors={"draft": "gray", "published": "green"}),
),
TableColumn(field="views", format=NumberFormat(compact=True)),
"published_at",
TableColumn(field="get_word_count", header="Words", format=NumberFormat),
]
ordering = ["-published_at"]
def get_word_count(self, obj, request):
return len(obj.body.split())
The first column opens the object. Editors sort on the columns backed by a
model field; sortable_by limits them. See
Table columns and
Format options.
2. Search and filter
# blog/admin.py (a fragment of ArticleStudio)
search_fields = ["title", "summary"]
list_filter = ["status", "category", "published_at", "featured"]
Each filter gets the control that fits its field: a choice list, Yes and
No, a date range with presets such as Last 7 days, or a searchable picker
for a relation. Filters can follow relations ("category__name"), and a
ListFilter class filters on anything a queryset can express. See
Filters.
3. Offer other views
Next to the table, a list page can show a list of readable items, a
calendar, a board and a gallery. Name the views in
list_views; editors switch with a toggle, and the admin remembers their
choice:
# blog/admin.py
from content_studio import register
from content_studio.admin import ModelStudio
from content_studio.list_views import BoardView
from .models import Article
@register(Article)
class ArticleStudio(ModelStudio):
publishing = "published_at"
list_views = ["table", "list", "calendar", BoardView("status"), "gallery"]
list_item_display = {
"title": "title",
"description": "summary",
"meta": "category",
"thumbnail": "cover",
}
- The list and the gallery show the parts of
list_item_display; the gallery shows thethumbnailas a tile. - The calendar puts articles on the day of their publish date, so
scheduled content shows on the day it goes live.
CalendarViewpicks another date field. - The board shows a column per status; dragging a card to another column changes it.
See Calendar, Board and Gallery.
4. Edit in the table
table_edit makes columns editable in place, without opening the edit
page. A cell saves when the editor leaves it, and a toast offers Undo:
# blog/admin.py (a fragment of ArticleStudio)
table_edit = ["category", "featured"]
The first column always opens the object, so it is never editable. See Editing in the table.
5. Act on objects
Editors select rows and run a bulk action, or use the ⋯ menu of one row.
Delete is built in; add your own with @bulk_action:
# blog/admin.py
from content_studio import register
from content_studio.actions import bulk_action
from content_studio.admin import ModelStudio
from .models import Article
@register(Article)
class ArticleStudio(ModelStudio):
allow_duplication = True
row_actions = ["feature", "duplicate", "delete"]
@bulk_action(label="Feature", icon="ph-bold ph-star")
def feature(self, request, queryset):
return queryset.update(featured=True)
A number returned reports how many objects changed. An action can ask for values first, such as a date or an editor to assign; see Bulk actions and Row actions. The trash and reordering are one option each.
6. Export and import
# blog/admin.py (a fragment of ArticleStudio)
export_fields = ["title", "category", "status", "views", "published_at"]
Editors then download every object that matches the search and filters as
a CSV file (Excel too, with the excel extra), and with
allow_import = True they create and update objects from a file. See
Exporting the list and
Importing from a file.
7. Put content on the dashboard
The dashboard is the admin's start page. Give it widgets that show content, by subclassing the built-in widget types:
# blog/dashboard.py
from content_studio.dashboard.content_list import ContentListWidget
from content_studio.dashboard.statistic import StatisticWidget
from .models import Article
class PublishedArticlesWidget(StatisticWidget):
def get_data(self, request):
return {
"title": "Published articles",
"value": Article.objects.filter(status="published").count(),
}
class LatestArticlesWidget(ContentListWidget):
def get_data(self, request):
articles = Article.objects.order_by("-published_at")[:5]
return {
"title": "Latest articles",
"content": [
{"model": "blog.article", "id": str(article.pk), "title": article.title}
for article in articles
],
}
# mysite/studio.py
from content_studio.admin import ContentStudioSite
from content_studio.dashboard import Dashboard
from content_studio.dashboard.activity_log import ActivityLogWidget
from blog.dashboard import LatestArticlesWidget, PublishedArticlesWidget
class BlogStudioSite(ContentStudioSite):
dashboard = Dashboard(
widgets=[
PublishedArticlesWidget(),
LatestArticlesWidget(),
ActivityLogWidget(),
]
)
admin_site = BlogStudioSite()
See Dashboard for the widget types, statistics with a trend and the task dashboard.
Large tables
On a table with millions of rows, counting every page gets slow. Set
list_count = "estimated" or "none" on the studio; see
Large tables.