Tweaks to coding style of model examples

pull/1310/head
Karl Hobley 2015-05-14 21:42:26 +01:00
rodzic 22ba2328a1
commit 175d147e09
2 zmienionych plików z 65 dodań i 81 usunięć

Wyświetl plik

@ -25,7 +25,7 @@ This example represents a typical blog post:
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from wagtail.wagtailadmin.edit_handlers import FieldPanel, MultiFieldPanel
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
@ -40,17 +40,13 @@ This example represents a typical blog post:
related_name='+'
)
BlogPage.content_panels = [
FieldPanel('title', classname="full title"),
content_panels = Page.content_panels [
FieldPanel('date'),
FieldPanel('body', classname="full"),
]
BlogPage.promote_panels = [
FieldPanel('slug'),
FieldPanel('seo_title'),
FieldPanel('show_in_menus'),
FieldPanel('search_description'),
promote_panels = [
MultiFieldPanel(Page.promote_panels, "Common page configuration"),
ImageChooserPanel('feed_image'),
]

Wyświetl plik

@ -48,21 +48,11 @@ Let's look at an example of a panel definition:
.. code-block:: python
COMMON_PANELS = (
FieldPanel('slug'),
FieldPanel('seo_title'),
FieldPanel('show_in_menus'),
FieldPanel('search_description'),
)
...
class ExamplePage(Page):
# field definitions omitted
...
ExamplePage.content_panels = [
FieldPanel('title', classname="full title"),
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
FieldRowPanel([
FieldPanel('start_date', classname="col3"),
@ -73,8 +63,8 @@ Let's look at an example of a panel definition:
PageChooserPanel('related_page'),
]
ExamplePage.promote_panels = [
MultiFieldPanel(COMMON_PANELS, "Common page configuration"),
promote_panels = [
MultiFieldPanel(Page.promote_panels, "Common page configuration"),
]
After the ``Page``-derived class definition, just add lists of panel definitions to order and organize the Wagtail page editing interface for your model.
@ -97,13 +87,13 @@ Wagtail provides a general-purpose WYSIWYG editor for creating rich text content
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel
# ...
class BookPage(Page):
book_text = RichTextField()
BookPage.content_panels = [
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
# ...
]
``RichTextField`` inherits from Django's basic ``TextField`` field, so you can pass any field parameters into ``RichTextField`` as if using a normal Django field. This field does not need a special panel and can be defined with ``FieldPanel``.
@ -122,7 +112,8 @@ One of the features of Wagtail is a unified image library, which you can access
from wagtail.wagtailimages.models import Image
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
# ...
class BookPage(Page):
cover = models.ForeignKey(
'wagtailimages.Image',
@ -132,9 +123,8 @@ One of the features of Wagtail is a unified image library, which you can access
related_name='+'
)
BookPage.content_panels = [
content_panels = Page.content_panels + [
ImageChooserPanel('cover'),
# ...
]
Django's default behavior is to "cascade" deletions through a ForeignKey relationship, which is probably not what you want happening. This is why the ``null``, ``blank``, and ``on_delete`` parameters should be set to allow for an empty field. (See `Django model field reference (on_delete)`_ ). ``ImageChooserPanel`` takes only one argument: the name of the field.
@ -153,7 +143,8 @@ For files in other formats, Wagtail provides a generic file store through the ``
from wagtail.wagtaildocs.models import Document
from wagtail.wagtaildocs.edit_handlers import DocumentChooserPanel
# ...
class BookPage(Page):
book_file = models.ForeignKey(
'wagtaildocs.Document',
@ -163,9 +154,8 @@ For files in other formats, Wagtail provides a generic file store through the ``
related_name='+'
)
BookPage.content_panels = [
BookPage.content_panels = Page.content_panels + [
DocumentChooserPanel('book_file'),
# ...
]
As with images, Wagtail documents should also have the appropriate extra parameters to prevent cascade deletions across a ForeignKey relationship. ``DocumentChooserPanel`` takes only one argument: the name of the field.
@ -180,7 +170,8 @@ You can explicitly link ``Page``-derived models together using the ``Page`` mode
from wagtail.wagtailcore.models import Page
from wagtail.wagtailadmin.edit_handlers import PageChooserPanel
# ...
class BookPage(Page):
publisher = models.ForeignKey(
'wagtailcore.Page',
@ -190,9 +181,8 @@ You can explicitly link ``Page``-derived models together using the ``Page`` mode
related_name='+',
)
BookPage.content_panels = [
content_panels = Page.content_panels + [
PageChooserPanel('related_page', 'demo.PublisherPage'),
# ...
]
``PageChooserPanel`` takes two arguments: a field name and an optional page type. Specifying a page type (in the form of an ``"appname.modelname"`` string) will filter the chooser to display only pages of that type.
@ -216,9 +206,8 @@ Snippets are vanilla Django models you create yourself without a Wagtail-provide
related_name='+'
)
BookPage.content_panels = [
content_panels = [
SnippetChooserPanel('advert', Advert),
# ...
]
See :ref:`snippets` for more information.
@ -327,8 +316,7 @@ Let's look at the example of adding related links to a ``Page``-derived model. W
class BookPage(Page):
# ...
BookPage.content_panels = [
# ...
content_panels = Page.content_panels + [
InlinePanel( 'related_links', label="Related Links" ),
]
@ -367,21 +355,21 @@ As standard, Wagtail organises panels into three tabs: 'Content', 'Promote' and
class BlogPage(Page):
# field definitions omitted
BlogPage.content_panels = [
content_panels = [
FieldPanel('title', classname="full title"),
FieldPanel('date'),
FieldPanel('body', classname="full"),
]
BlogPage.sidebar_content_panels = [
sidebar_content_panels = [
SnippetChooserPanel('advert', Advert),
InlinePanel('related_links', label="Related links"),
]
BlogPage.edit_handler = TabbedInterface([
ObjectList(BlogPage.content_panels, heading='Content'),
ObjectList(BlogPage.sidebar_content_panels, heading='Sidebar content'),
ObjectList(BlogPage.promote_panels, heading='Promote'),
ObjectList(BlogPage.settings_panels, heading='Settings', classname="settings"),
edit_handler = TabbedInterface([
ObjectList(content_panels, heading='Content'),
ObjectList(sidebar_content_panels, heading='Sidebar content'),
ObjectList(Page.promote_panels, heading='Promote'),
ObjectList(Page.settings_panels, heading='Settings', classname="settings"),
])