Model Choosers for Wagtail admin
wagtail-modelchooser-1.13.1的Python项目详细描述
wagtail的插件,提供ModelChooserPanel和ModelChooserBlock 对于任意模型。
安装
使用pip安装:
pip install wagtail-modelchooser
然后将其添加到您的INSTALLED_APPS:
INSTALLED_APPS=[# ...'wagtailmodelchooser',# ...]
它适用于Wagtail 1.4及以上。
快速启动
要为模型启用选择器,必须注册模型。 对于简单的例子,用@register_model_chooser:
装饰您的模型fromdjango.dbimportmodelsfromwagtailmodelchooserimportregister_model_chooser@register_model_chooserclassAuthor(models.Model):name=models.CharField(max_length=255)def__str__(self):# The ``str()`` of your model will be used in the chooserreturnself.name
然后可以在编辑处理程序定义中使用ModelChooserPanel, 或者StreamField定义中的ModelChooserBlock:
fromwagtail.wagtailcore.blocksimportRichTextBlockfromwagtail.wagtailcore.fieldsimportStreamFieldfromwagtail.wagtailcore.modelsimportPagefromwagtail.wagtailadmin.edit_handlersimportFieldPanel,StreamFieldPanelfromwagtailmodelchooser.blocksimportModelChooserBlockfromwagtailmodelchooser.edit_handlersimportModelChooserPanelclassBook(Page):name=models.CharField(max_length=255)author=models.ForeignKey(Author)content_panels=[FieldPanel('name'),ModelChooserPanel('author'),]classContentPage(Page):body=StreamField([('text',RichTextBlock()),('author',ModelChooserBlock('books.Author')),])content_panels=[StreamFieldPanel('body'),]