[Grok-dev] object relationships
Jeroen Michiel
jmichiel at yahoo.com
Thu Jan 29 11:17:44 EST 2009
OK, I got it working!
This is how I managed it:
First of all, the IIntIds utility and a RelationCatalog need to be
registered, so I made an event subscription to grok.IObjectAddedEvent to do
this:
@grok.subscribe(Test, grok.IObjectAddedEvent)
def added(app, event):
app['intids'] = intids = IntIds()
grok.getSite().getSiteManager().registerUtility(intids,
provided=IIntIds)
app['catalog'] = catalog = relationfield.RelationCatalog()
grok.getSite().getSiteManager().registerUtility(catalog,
provided=ICatalog)
you also need an IObjectPath implementation, so I did this:
class ObjectPath(grok.GlobalUtility):
grok.provides(IObjectPath)
def path(self, obj):
return path(grok.getSite(), obj)
def resolve(self, path):
return resolve(grok.getSite(), path)
Then I have some data I want to refer to:
class IReferedData(Interface):
name = schema.TextLine(title=u'The Name')
class ReferedData(grok.Model):
grok.implements(IReferedData, IHasIncomingRelations)
def __init__(self, Name):
self.name = Name
The the data that has a reference to ReferedData:
class ITestData(Interface):
rel = relationfield.Relation(title=u'Relation')
class TestData(grok.Model):
grok.implements(ITestData, HasOutgoingRelations)
and in my add form I do this:
class Add(grok.AddForm):
grok.context(Interface)
form_fields = grok.Fields(ITestData,
select=schema.Choice(title=u'Relation', source=TestSource())).omit('rel')
@grok.action('Add')
def Add(self, **data):
testdata = TestData()
intids = component.getUtility(IIntIds)
id = intids.getId(data['select'])
del data['select']
self.applyData(testdata, **data)
testdata.rel = relationfield.RelationValue(id)
grok.getSite()['testdata'][str(len(grok.getSite()['testdata']))] =
testdata
self.redirect(self.url(testdata))
(the applyData is strictly not necessary, but will be if there would be
other fields associated to ITestData)
And that seems to do it! I can search relations and everything.
Anyone who has any remarks about things I should have done differently
perhaps, let me know, I can only learn from it.
--
View this message in context: http://www.nabble.com/object-relationships-tp21706997p21730138.html
Sent from the Grok mailing list archive at Nabble.com.
More information about the Grok-dev
mailing list