Phillip B Oldham wrote:
> Hi all. I'm playing with standalone ZODB at the moment trying to get a
> better understanding of its use in applications. I come from a
> PHP/MySQL background, and I'm taking my first steps with Python at the
> same time.

Heh, and just when people are moving more and more to ORMs like 
SQLAlchemy ;-)

> One of the things I'm not understanding about ZODB is assigning
> incremental IDs to objects.

How important is it that they're strictly incremental?
If it is, you may want to look at the BTrees.Length module.
You can treat a Length a bit like a relational database sequence, 
provided you keep incrementing each time you use the Length's value as 
an id.

> For instance, if I were to be writing a
> support-ticket system I'd want to give each ticket a unique number,
> but one that's "human-useable" (otherwise you could just use a UUID -
> try giving one of those over the phone!).

Well, the UUID you'd be thinking of would best be just the ZODB OID ;-)

> class Ticket(Persistence):
>   def __init__(self):
> = '' # How do I add a new incremental ID here?
> # and later on in the app
> tkt = Ticket()
> dbroot[?????] = tkt

Well, if it was just plain ZODB, I'd do it as follows:

from persistent import Persistent
from IOBTrees import IOTree
from BTrees.Length import Length

class Ticket(Persistent):

class TicketContainer(IOBTree):

   def __init__(self,id):
     self.next_id = Length()

   def addIssue(self):
     id = self.next_id()
     t = self[id] = Ticket()
     return t


It's very rare that a ticket needs to know its own id. If it does, it 
should ask its container.



Simplistix - Content Management, Zope & Python Consulting
Zope maillist  -
**   No cross posts or HTML encoding!  **
(Related lists - )

Reply via email to