让我今天有一个基于Archtypes的对象,其中包含一个多值引用字段ReferenceField,如下所示:
schema = BaseSchema.copy() + Schema((
ReferenceField(
'SomeObjects',
multiValued=1,
allowed_types=('SomeObjects',),
relationship = 'ThisObjectSomeObject',
)
))
class ThisObject(BaseContent):
schema = schema
如何使用代码添加对SomeObjects
字段的引用?
答案 0 :(得分:2)
抓取字段的值,修改它并设置字段。您可以直接添加实例,也可以使用UID字符串。
这样的事情:
val = instance.getSomeObjects()
val = val if val else []
val.append(uid)
instance.setSomeObjects(val)
答案 1 :(得分:1)
此示例使用File
内容类型和plone的relatedItems
字段,向您展示ReferenceField如何工作
假设plone
是你的Plone网站。
# Create two files
>>> plone.invokeFactory('File', 'file1')
file1
>>> plone.invokeFactory('File', 'file2')
file2
# Set reference from file1 to file2
# Since relatedItems is also multi valued like your field you have to pass a list of objects.
>>> plone.file1.setRelatedItems([file2])
# With the `regular` getter you got the objects
>>> plone.file1.getRelatedItems()
[<ATFile at /Plone/file2>]
# With the special raw getter you got the UID of the objects
>>> plone.file1.getRawRelatedItems()
['e1486e46376545db92f911c48dae5c69']
# You can also pass the UID of the object to the setter
plone.fil1.setRelatedItems([file2.UID()])
>>> plone.file1.getRelatedItems()
[<ATFile at /Plone/file2>]
答案 2 :(得分:0)
我假设你在这里问你如何以编程方式从ThisObject的实例中保存对另一个对象的引用?
如果是这样,你只需要传入你想要引用的对象的UID:
object.setSomeObjects(otherObject.UID())