浏览代码

Added docs about abtract model for InlinePanel (#9887)

Vallabh 2 年之前
父节点
当前提交
123d944674
共有 1 个文件被更改,包括 28 次插入0 次删除
  1. 28 0
      docs/topics/pages.md

+ 28 - 0
docs/topics/pages.md

@@ -366,6 +366,34 @@ class BlogPageRelatedLink(Orderable):
     ]
     ]
 ```
 ```
 
 
+In the above example, the `BlogPageRelatedLink` model can also be refactored into an abstract model:
+
+```python
+from django.db import models
+from modelcluster.fields import ParentalKey
+from wagtail.models import Orderable
+
+# The abstract model for related links, complete with panels
+class RelatedLink(models.Model):
+    name = models.CharField(max_length=255)
+    url = models.URLField()
+
+    panels = [
+        FieldPanel('name'),
+        FieldPanel('url'),
+    ]
+
+    class Meta:
+        abstract = True
+
+# The real model which combines the abstract model
+class BlogPageRelatedLink(Orderable,RelatedLink):
+    page = ParentalKey("wagtailcore.Page", on_delete=models.CASCADE, related_name='related_links')
+
+```
+
+Notice the change of `ParentalKey` to `"wagtailcore.Page"` model. The `related_links` relation will be set up on the base Page model, which means it will be available on all page types. Most likely we don't want to use it on all page types (for example, related links are not very useful on a homepage).
+
 To add this to the admin interface, use the {class}`~wagtail.admin.panels.InlinePanel` edit panel class:
 To add this to the admin interface, use the {class}`~wagtail.admin.panels.InlinePanel` edit panel class:
 
 
 ```python
 ```python