2014-09-30 45 views
0

讓我們的一天,我已經有了一個多值的參考場ReferenceField按照下面的拱形物基於對象:如何使用代碼添加對多值ReferenceField的引用?

schema = BaseSchema.copy() + Schema((
    ReferenceField(
     'SomeObjects', 
     multiValued=1, 
     allowed_types=('SomeObjects',), 
     relationship = 'ThisObjectSomeObject', 
    ) 
)) 

class ThisObject(BaseContent): 
    schema = schema 

我如何去增加一個參考SomeObjects領域使用的代碼?

+1

你是什麼意思與「如何」? Plone - >添加菜單 - >您的內容 - >選擇參考?或者在測試中?或調試控制檯?你的內容是否正常工作? – Mathias 2014-09-30 12:01:35

+0

我編輯了這個問題,以反映我想知道如何使用代碼來實現這一事實。 – juriejan 2014-10-01 11:33:58

+0

好的,那麼你已經有兩個答案。我會添加第三個:-) – Mathias 2014-10-01 11:52:00

回答

2

抓住字段的值,修改它,並設置現場。您可以直接添加實例,也可以使用UID字符串。

事情是這樣的:

val = instance.getSomeObjects() 
val = val if val else [] 
val.append(uid) 
instance.setSomeObjects(val) 
+0

設置一個多值參考字段與一個單一的值而不是支持列表。 – Campbell 2014-10-01 01:05:11

0

我在這裏假設你問你如何以編程方式保存從ThisObject的實例中的另一個對象的引用?

如果是這樣,你只需要在對象的UID傳給你想參考:

object.setSomeObjects(otherObject.UID()) 
+1

不記得它也可以,但可能你必須通過一系列的用戶界面 – 2014-09-30 15:54:36

+0

...我的意思是:因爲它是多值的 – 2014-09-30 16:18:57

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>] 
相關問題