[Grok-dev] Re: Referencing objects by id

Martijn Faassen faassen at startifact.com
Tue Aug 5 14:11:59 EDT 2008


Hello,

Gerard Petersen wrote:
> I do wonder about the 'id' though. I want the customer object to have a 
> numeric value attribute for administering relations to email 
> correspondence, invoices, tickets, etc.

I have the suspicion we're talking about a number of topics at the same 
time:

* naming in containers, i.e. names in URLs

* references from one object to another

If you have a single container which contains all the objects you may 
want to reference to, of course you could use the container key (or some 
id attribute) as the reference. There are other options however.

If your customer objects could occur throughout the ZODB in some form 
hierarchy it might be worthwhile to investigate the use of the IIntIds 
utility. This is a utility that can be used to give all objects a 
globally unique id in the system, which you can then refer to, and resolve.

Another option would be to exploit the underlying nature of the object 
database itself, and just put in a plain Python reference to the 
customer object in whatever object that needs to refer to it. Of course 
this might be too tight a coupling - what do you do when the customer 
goes away for instance?

There are also more advanced solutions which perhaps require a bit too 
much puzzling right now, but can help in more complicated scenarios 
where you may want to ask questions about what relations exist in your 
system. One library I wrote (but needs more polishing) is z3c.relationfield:

http://svn.zope.org/z3c.relationfield/trunk/

http://svn.zope.org/z3c.relationfield/trunk/src/z3c/relationfield/README.txt

> By reading your explanation on the __name__ identifier (key?) I can 
> choose any name to uniquely identify one customer object from many in a 
> container. Right?

You could use whatever strategy you like. A container is like a 
dictionary in this; as long as your keys identify the values in a unique 
way they could be any string.

> Personally I don't care about whether it's a name or a number behind the 
> scenes, as long as it's the Customers name (and what not) that is 
> showing on the webpage.
> 
> But since it can be anything I want, how would one then name an object's 
> key to keep it understandable for humans (the coders that is)?
> Any best practice tips on that are much appreciated.

It depends a bit on who is in control of these names. In some 
applications, for some objects, you want to leave your users in control. 
I could for instance imagine the customers would have to supply a unique 
user name, and you could use this as the key. This makes for easy to 
remember URLs, which is nice. A content management system that allows 
you to create documents usually allows this.

If the user is not controlling the name in the URL, you could just make 
up some number (the NameChooser bit might be useful here, though not 
strictly necessary - it's a general abstraction for making up names in 
containers) and make sure it's not in use yet.

You could also create a name based on data of the object itself, if 
you're reasonably sure it's mostly unique and think it would provide for 
prettier URLs. You could for instance use the name of the customer in 
this case, perhaps lowercased, spaces replaced by underscores, with a 
'unique-maker' number in the end if necessary, and replacing any 
characters that wouldn't look nice in the URL. NameChooser could again 
be something you fit this behavior into.

Regards,

Martijn



More information about the Grok-dev mailing list