添加新的Wagtail CMS Snippets(Adding new Wagtail CMS Snippets)

我想添加新的Wagtail Snippet模型,但找不到任何关于正确文件命名的文档来开始构建它们; 我将它们放在我的应用程序model.py文件中,还是有类似于wagtailadmin的特定方法? 谢谢。

I would like to add new Wagtail Snippet models but cannot find any documentation regarding proper file naming to begin building them; do I place them in my apps model.py file or does it have a specific method similar to wagtailadmin? Thank you.

最满意答案

片段是常见的django模型,使用装饰器功能进行注册。 因此他们住在models.py 。

from django.db import models from wagtail.wagtailsnippets.models import register_snippet @register_snippet class Foobar(models.Model): foo = models.CharField(max_length=3)

如果您的应用程序增长,您可以考虑使用包而不是模块。 创建一个名为models的文件夹,并将models.py的内容复制到名为__init__.py的文件中。 然后创建单独的模块。 例如,在这个新文件夹中的snippets.py并将它们导入__init__.py

示例代码:

models/__init__.py

from .snippets import *

models/snippets.py

from django.db import models from wagtail.wagtailsnippets.models import register_snippet @register_snippet class Foobar(models.Model): foo = models.CharField(max_length=3)

Snippets are common django models, which are registered using a decorator function. Therefore they live in models.py.

from django.db import models from wagtail.wagtailsnippets.models import register_snippet @register_snippet class Foobar(models.Model): foo = models.CharField(max_length=3)

If your app grows you might consider using a package instead of a module. Create a folder called models and copy the contents of models.py into a file called __init__.py. Afterwards create separate modules. E.g. snippets.py inside of this new folder and import them inside of __init__.py

Sample code:

models/__init__.py:

from .snippets import *

models/snippets.py:

from django.db import models from wagtail.wagtailsnippets.models import register_snippet @register_snippet class Foobar(models.Model): foo = models.CharField(max_length=3)

更多推荐