在我的一个Plone网站中,我有一些灵巧模型用于生成字母.模型是:"模型"(字母的基本内容),"联系人"(包含联系信息,如姓名,地址等)和"合并"(这是一个模拟对象,在其中我们替换一些模型的一部分与收件人信息)."Merge"对象的模式如下:
class IMergeSchema(form.Schema):
"""
"""
title = schema.TextLine(
title=_p(u"Title"),
)
form.widget(text='plone.app.z3cform.wysiwyg.WysiwygFieldWidget')
text = schema.Text(
title=_p(u"Text"),
required=False,
)
form.widget(recipients=MultiContentTreeFieldWidget)
recipients = schema.List(
title=_('label_recipients',
default='Recipients'),
value_type=schema.Choice(
title=_('label_recipients',
default='Recipients'),
# Note that when you change the source, a plone.reload is
# not enough, as the source gets initialized on startup.
source=UUIDSourceBinder(portal_type='Contact')),
)
form.widget(model=ContentTreeFieldWidget)
form.mode(model='display')
model = schema.Choice(
title=_('label_model',
default='Model'),
source=UUIDSourceBinder(portal_type='Model'),
)
Run Code Online (Sandbox Code Playgroud)
创建新的"合并"对象时,我希望预设"收件人"字段,并在创建新对象的文件夹中提供所有可用的联系人.我按照Martin Aspelli的指南为字段添加默认值:http://plone.org/products/dexterity/documentation/manual/developer-manual/reference/default-value-validator-adaptors
它适用于文本输入字段,但我不能让它适用于"收件人"字段.生成默认值的方法如下(一些调试信息带有丑陋的打印,但稍后会删除它们);):
@form.default_value(field=IMergeSchema['recipients'])
def all_recipients(data):
contacts = [x for x in data.context.contentValues()
if IContact.providedBy(x)]
paths = [u'/'.join(c.getPhysicalPath()) for c …Run Code Online (Sandbox Code Playgroud) 我正在为 Django 项目构建一个小型搜索系统(是的,我知道,已经有很多产品在这样做,但我想尝试一下,只是为了好玩)。我基本上有以下型号:
class Word(models.Model):
""" A searchable word.
We only store the slugified value
"""
slug = models.SlugField(unique = True)
class Searchable(models.Model):
""" Superclass for Searchable objects.
"""
words = models.ManyToManyField(
Word,
through='WordCount')
class WordCount(models.Model):
""" Occurences of a word in a Searchable object.
"""
word = models.ForeignKey(Word)
item = models.ForeignKey(Searchable)
count = models.IntegerField()
Run Code Online (Sandbox Code Playgroud)
例如,我使用文本“Hello StackOverflow,我有一个 Django 问题”创建了一个对象 Page(子类化 Searchable)。系统将为这句话中的每个单词创建一个 Word 实例,并为每个单词创建一个 WordCount 实例,表示每个单词在文本中出现一次。
进行查询以获取包含一个更多单词的所有 Searchable 实例工作正常(searchable_text 提取单词并从中创建一个列表):
def search(query)
tokens = searchable_text(query)
words = Word.objects.filter(
reduce(operator.or_,
[models.Q(slug__contains = …Run Code Online (Sandbox Code Playgroud)