0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00
wagtail/docs/advanced_topics/reference_index.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
2.6 KiB
Markdown
Raw Normal View History

2023-02-03 18:42:02 +01:00
(managing_the_reference_index)=
2023-02-10 23:13:45 +01:00
2023-01-21 08:11:14 +01:00
# Managing the Reference Index
Wagtail maintains a reference index, which records references between objects whenever those objects are saved. The index allows Wagtail to efficiently report the usage of images, documents and snippets within pages, including within StreamField and rich text fields.
## Configuration
2023-04-18 15:00:35 +02:00
By default, the index will store references between objects managed within the Wagtail admin, specifically:
- all Page types
- Images
- Documents
- models registered as Snippets
- models registered with ModelAdmin
2023-04-18 15:00:35 +02:00
```{versionchanged} 5.0
When introduced in Wagtail 4.1, the Reference Index recorded references in all application models by default. Wagtail 5.0 reduced the scope of the default models to those specifically linked with Wagtail.
```
The reference index does not require any further configuration. However there are circumstances where it may be necessary to add or remove models from the index.
(registering_a_model_for_indexing)=
### Registering a Model for Indexing
A model can be registered for reference indexing by adding code to `apps.py` in the app where the model is defined:
```python
from django.apps import AppConfig
class SprocketAppConfig(AppConfig):
...
def ready(self):
from wagtail.models.reference_index import ReferenceIndex
from .models import SprocketController
ReferenceIndex.register_model(SprocketController)
```
### Preventing Indexing of models and fields
2023-01-21 08:11:14 +01:00
The `wagtail_reference_index_ignore` attribute can be used to prevent indexing with a particular model or model field.
- set the `wagtail_reference_index_ignore` attribute to `True` within any model class where you want to prevent indexing of all fields in the model; or
- set the `wagtail_reference_index_ignore` attribute to `True` within any model field, to prevent that field or the related model field from being indexed:
2023-01-21 08:11:14 +01:00
```python
class CentralPage(Page):
...
reference = models.ForeignKey(
"doc",
on_delete=models.SET_NULL,
related_name="page_ref",
)
reference.wagtail_reference_index_ignore = True
...
```
## Maintenance
2023-04-18 15:00:35 +02:00
The index can be rebuilt with the `rebuild_references_index` management command. This will repopulate the references table and ensure that reference counts are displayed accurately. This should be done if models are manipulated outside of Wagtail, or after an upgrade.
A summary of the index can be shown with the `show_references_index` management command. This shows the number of objects indexed against each model type, and can be useful to identify which models are being indexed without rebuilding the index itself.