0

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.

4

1 回答 1

1

片段是常见的 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)
于 2017-02-13T20:20:48.037 回答