Skip to main content

📝 Forms

The edit page is the form editors fill in. This guide builds the article's edit page step by step: a layout of form sets and a sidebar, the right widget for each field, fields that show only when they apply, validation, related objects and inlines. Every option is in Edit page layout and Widgets.

The guide uses this model:

# blog/models.py
from django.db import models

from content_studio.media_library.fields import MediaField


class Category(models.Model):
name = models.CharField(max_length=100)

def __str__(self):
return self.name


class Article(models.Model):
class Status(models.TextChoices):
DRAFT = "draft", "Draft"
PUBLISHED = "published", "Published"

title = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
summary = models.TextField(blank=True)
body = models.TextField(blank=True)
cover = MediaField()
category = models.ForeignKey(
Category, null=True, blank=True, on_delete=models.SET_NULL
)
status = models.CharField(
max_length=20, choices=Status.choices, default=Status.DRAFT
)
published_at = models.DateTimeField(null=True, blank=True)
featured = models.BooleanField(default=False)
featured_until = models.DateField(null=True, blank=True)

def __str__(self):
return self.title


class Review(models.Model):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
text = models.TextField()
stars = models.PositiveSmallIntegerField(default=5)

1. Lay out the page​

Without a layout, the edit page shows every field in the order the model declares them. Group them with edit_main, the wide column, and edit_sidebar, the narrow one:

# blog/admin.py
from content_studio import register
from content_studio.admin import ModelStudio
from content_studio.form import Field, FieldLayout, FormSet

from .models import Article


@register(Article)
class ArticleStudio(ModelStudio):
edit_main = [
FormSet(
title="Content",
description="What readers see",
fields=[
"title",
FieldLayout(fields=["slug", "category"], columns=2),
"summary",
"body",
],
),
FormSet(title="Cover", fields=["cover"]),
]
edit_sidebar = ["status", "published_at", "featured", "featured_until"]
  • A FormSet is a titled card of fields.
  • A FieldLayout puts fields side by side; on a phone they stack.
  • A plain string is short for Field(name).

Every required field must be on the page: the edit page only saves the fields it shows, as in the classic Django admin. Run manage.py check; W001 names a required field you left out. See Edit page layout.

2. Pick the widgets​

Each field type has a default widget: a date picker for a DateField, a searchable picker for a foreign key, the media picker for a MediaField. Choose another one with Field(widget=...), and pass an instance for options:

# blog/admin.py
from content_studio import register, widgets
from content_studio.admin import ModelStudio
from content_studio.form import Field, FormSet

from .models import Article


@register(Article)
class ArticleStudio(ModelStudio):
edit_main = [
FormSet(
title="Content",
fields=[
Field(
"title",
widget=widgets.InputWidget(placeholder="A short, clear title"),
),
"slug",
Field("summary", widget=widgets.TextAreaWidget(rows=3)),
Field("body", widget=widgets.MarkdownWidget),
],
),
FormSet(
title="Cover",
fields=[Field("cover", widget=widgets.CoverImageWidget)],
),
]
edit_sidebar = [
Field("status", widget=widgets.ToggleGroupWidget),
"published_at",
Field("featured", widget=widgets.SwitchWidget),
"featured_until",
]

For rich text, use the built-in HTMLField on the model: it gets the rich text editor with images, tables and links to other objects. For structured page content, see Block fields. When no widget fits, write your own; see Custom widgets and formats.

3. Show fields only when they apply​

when takes a Django Q object over the form's values and shows the field, form set or component only while it matches. The edit page applies it as the editor types:

# blog/admin.py
from django.db.models import Q

from content_studio import register
from content_studio.admin import ModelStudio
from content_studio.form import Field

from .models import Article


@register(Article)
class ArticleStudio(ModelStudio):
edit_sidebar = [
"status",
Field("published_at", when=Q(status=Article.Status.PUBLISHED)),
"featured",
Field("featured_until", when=Q(featured=True)),
]

A hidden field is not saved: its stored value stays as it is. Conditions decide what the page shows, not who may change what; use read-only fields and permissions for that. See Conditional fields for the lookups you can use.

4. Fill in fields for the editor​

  • Prepopulated fields follow other fields while the object is new: prepopulated_fields = {"slug": ("title",)} (Prepopulated fields).
  • Read-only fields show a value without an input: readonly_fields = ["published_at"], or Field("published_at", readonly=True) (Read-only fields).
  • Save hooks set what the form does not show, such as the author; see Saving and deleting.

5. Validate​

Saving runs the model's validation, as the classic Django admin does: clean(), unique fields and the model's constraints. Raise a ValidationError keyed by field, and the edit page shows the message on that field:

# blog/models.py (a fragment)
from django.core.exceptions import ValidationError


class Article(models.Model):
# ... the fields above ...

def clean(self):
if self.featured and not self.cover_id:
raise ValidationError({"cover": "A featured article needs a cover."})

See Model validation and Unique fields.

The category picker searches the categories, and when the one an editor needs does not exist yet, creates it in a dialog without leaving the page. That only takes a studio for Category:

# blog/admin.py
from content_studio import register
from content_studio.admin import InlineModelStudio, ModelStudio

from .models import Article, Category, Review


@register(Category)
class CategoryStudio(ModelStudio):
search_fields = ["name"]


class ReviewInline(InlineModelStudio):
model = Review
table_display = ["text", "stars"]


@register(Article)
class ArticleStudio(ModelStudio):
inlines = [ReviewInline]

ReviewInline edits the article's reviews on its edit page, with the columns of its table_display. See Related objects and Inlines.

7. Saving​

Editors save with Save, Save and add another or Save and close, or with Ctrl S (⌘S on macOS). The edit page keeps unsaved changes in the browser and offers them back after a crash or a closed tab; turn that off for a model with recover_changes = False. See Saving and Unsaved changes.

Next steps​

  • Limit a field's options by another field on the form, such as the cities of the chosen country, with dependent choices.
  • Put buttons, text, an SEO preview or callouts on the page with components.
  • Ask for values before an action runs with action forms.
  • Add publishing and review, which add their own controls to the edit page.