在编辑表单中,什么会导致门户目录在自动完成选项字段的源对象中失败?

2024-04-29 15:12:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个选择字段的源对象,它是一个自动完成小部件,它依赖于使用portal catalog根据用户传入的值查找内容。不幸的是,在查看编辑表单时,门户目录有时会返回0个结果,而它应该返回1个结果。你知道吗

应该返回1个结果的函数是“getTerm”。我做了一个print语句来查看它得到了多少结果,并确保传入的值是术语的值。我的print语句显示传入的值总是应该的值,但它并不总是找到结果。我不知道为什么这是'失败',当它在添加形式的工作。你知道吗

我的界面:

class IMyContentType(model.Schema):

    organization = schema.Choice(title='',
                                 source=Organizations(),
                                )

我的对象源:

class OrganizationsSource(object):
    implements(IQuerySource)

    def __init__(self,context):
        self.context = context

    def queryOrganizations(self,value):
        catalog = api.portal.get_tool(name='portal_catalog')
        brains = catalog.evalAdvancedQuery(
               AdvancedQuery.MatchRegexp('portal_type','Organization') &
               AdvancedQuery.MatchRegexp('Title',value+"*")
             )
        return [i.Title for i in brains]

    def __contains__(self,value):
        q = self.queryOrganizations(value)
        if len(q) > 0:
            return True
        else:
            return False

    def getTerm(self, value):
        q = self.queryOrganizations(value)
        #Where I check to see if it should be working
        #the value passed in is the one that should be
        print value, len(q)
        return SimpleTerm(title=q[0],value=q[0])

    def getTermByToken(self,token):
        return self.getTerm(token)

    def search(self,query_string):
        q = self.queryOrganizations(query_string)
        return [SimpleTerm(title=v,value=v,token=v) for v in q]

class Organizations(object):
    implements(IContextSourceBinder)

    def __init__(self):
        self.context = self

    def __call__(self, context):
        return OrganizationsSource(context)

这种方法可行吗?我应该用别的目录吗?你知道吗

我还在getTerm函数中尝试了一个简单的searchResults,而不是evalAdvancedQuery:

def getTerm(self,value):
    catalog = api.portal.get_tool(name='portal_catalog')
    brains = catalog.searchResults({'portal_type':'Organization',
                                   'Title':value,
                                 })
    return SimpleTerm(title=brains[0]['Title'],
                      value=brains[0]['Title'],
                  )

我也遇到了同样的问题。你知道吗

我正在使用Plone 5.1。你知道吗


Tags: inselfreturntitlevaluedefcontextclass
1条回答
网友
1楼 · 发布于 2024-04-29 15:12:22

首先,我建议您使用ZCatalog而不是AdvancedQuery。 对于您正在做的事情,没有理由使用AdvancedQuery。 只需使用普通目录通过plone.api文件https://docs.plone.org/develop/plone.api/docs/content.html#find-content-objects 还要确保用户具有查看您正在搜索的对象所需的权限。你知道吗

示例:

from plone import api

def query_organizations(self, search_term):
    search_term = search_term and search_term + '*' or ''
    documents = api.content.find(
        portal_type='Organization',
        Title=search_term,
    )

相关问题 更多 >