When working with Wagtail CMS, the admin interface is where you'll spend a lot of your time. And guess what makes that interface so flexible and powerful? It's the FieldPanels! In this lesson, we'll dive into what FieldPanels are, why they're important, and how to add one to your Page Model.
## Introduction to FieldPanels
FieldPanels in Wagtail serve as a bridge between your Page Model and the admin interface. They define how a particular field from the model should appear and behave when you're editing a page in the Wagtail admin.
### What Do They Do?
- **Field Mapping**: Maps a model field to a widget in the admin interface.
- **Customization**: Allows you to specify how the field should be displayed, whether as a simple text box, a rich text editor, or even a file upload area.
## Importance of FieldPanels
FieldPanels play a critical role in making Wagtail user-friendly and flexible. Here's why:
- **Content Customization**: FieldPanels allow content editors to focus on the content, not the code. They can easily input and edit data without needing to touch the backend code.
- **Data Management**: By specifying the types of data that can go into each field, FieldPanels ensure that the data remains consistent and manageable.
## Adding a FieldPanel
Adding a FieldPanel to your Page Model is pretty straightforward. Let's say you have a `BlogPage` model and you want to add a new `subtitle` field. Here's how you'd do it:
1. **Update Your Model**: First, add the new field to your model.
```python
from wagtail.models import Page
from wagtail.fields import RichTextField
from wagtail.admin.panels import FieldPanel
class BlogPage(Page):
subtitle = models.Field(blank=True)
```
2. **Add FieldPanel**: Next, update the `content_panels` list to include your new field.
```python
content_panels = Page.content_panels + [
FieldPanel('subtitle'),
]
```
3. **Migrate**: Don't forget to make and apply your migrations.
```bash
./manage.py makemigrations
./manage.py migrate
```
4. **Check Admin Interface**: Once you've made these changes and migrated your database, you should see the new `subtitle` FieldPanel when you edit a `BlogPage` in the Wagtail admin.
## Read Only FieldPanels
If you want to display information in the admin, but don't want it to be editable by anybody, you can tell the FieldPanel to be `read_only`.
```python
class HomePage(Page):
subtitle = models.CharField(blank=True, max_length=100)
content_panels = Page.content_panels + [
FieldPanel('subtitle', read_only=True),
]
```