سؤال

im developing an application that it is used to fill some huge forms. There are several projects that a form can belong to. Also the form has two sections a that can be filled in many times, like objectives and activities, so a form can have many objectives and activities defined.

I have a class to represent the projects, another for the form and two simple classes to represent the objective and activities. Project has a list of forms, and Form has a list of activities and objectives.

class Project(persistent.Persistent):
    forms = PersistentList() 
    ...

class Form(persistent.Persistent):
    objectives = PersistentList() 
    activities = PersistentList() 
    ...

My question is, im planning on storing this data in ZODB like this:

db['projects']   = OOBTree()
db['forms']      = OOBTree()
db['activities'] = OOBTree()
db['objectives'] = OOBTree()

project = Project(...)//fill data with some parameters
form = Form(...)//fill data with some parameters

objective1 = Objective(...)
objective2 = Objective(...)
activity1 = Activitiy(...)
activity2 = Activitiy(...)

form.addObjective(objective1)
form.addObjective(objective2)
form.addActivity(activity1)
form.addActivity(activity2)

project.addForm(form)

db['projects']['projectID'] = project
db['forms']['formID'] = form
db['activities']['activityID'] = activity1
db['activities']['activityID'] = activity2
db['objectives']['objectiveID'] = objective1
db['objectives']['objectiveID'] = ojective2

transaction.commit()

I know that when storing the project, the list of forms gets persisted as well, and the corresponding list of objectives and activities from the form too.

But what happens in the case of the other OOBTrees, 'forms', 'activities' and 'objectives'? im doing this in order to be easier to traverse or look for individual forms/objectives/activities. But im not sure if the ZODB will cache those objects and only persist them once when saving the project, and keeping a reference to that object. So when any of those are modified, all references are updated. Meaning that when doing db['forms']['formID'] = form the OOBTree will point to the same object as the project OOBTree and thus not persisting the same object twice. Is that the way it works? or ill get duplicated persisted objects and will all be independent instances?

I know that theres repoze catalog to handle indexing and stuff, but i dont need that much, just be able to access a form without having to iterate over projects.

Thanks!

هل كانت مفيدة؟

المحلول

Yes, as long as the target objects you are storing have classes that subclass persistent.Persistent somewhere in their inheritance, any references to the same object will point to exactly the same (persistent) object. You should not expect duplication as you have described this.

The short-long-version: ZODB uses special pickling techniques, when serializing the source/referencing object, it sees that the reference is to a persistent object, instead of storing that object again, it stores a tuple of the class dotted name and the internal OID of the target object.

Caveat: this only works within the same object database. You should not have cross-database references in your application.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top