[Plone-Users] ReferenceField Accessor

3 Messages Forum Options Options
Embed this topic
Permalink
Mastro, James
[Plone-Users] ReferenceField Accessor
Reply Threaded MoreMore options
Print post
Permalink
I have been using ReferenceField's in my Archetype classes a lot lately.  As
I understand it (and I'm hoping that someone will tell me I'm wrong), one
can *not* simply write:
 
value = object.getMyReferenceFieldName()
 
So, when I want to access *only* the first reference (which is most of the
time because rarely do I store a list in such field), I do something like
the following:
 
def getFirstReference(object=None, reference=None):
    return_object = None
    if object and relationship:
        references = object.getReferences(relationship)
        if references:
            if references[0]:
                return_object = references[0]
       
    return return_object

This seems a bit excessive to me.  Is there an easier/faster way to make
such access?

Thanks,

Jim

 


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Plone-Users mailing list
Plone-Users@...
https://lists.sourceforge.net/lists/listinfo/plone-users
Rob Miller
Re: [Plone-Users] ReferenceField Accessor
Reply Threaded MoreMore options
Print post
Permalink
Mastro, James wrote:
> I have been using ReferenceField's in my Archetype classes a lot lately.  As
> I understand it (and I'm hoping that someone will tell me I'm wrong), one
> can *not* simply write:
>  
> value = object.getMyReferenceFieldName()

this should work.  if you are using a multi-valued reference field, then this
will return a list of referenced objects (or possibly a tuple, i forget), but
i think if it's a single valued field, then the referenced object, if any,
should be what is returned.  have you tried it?

> So, when I want to access *only* the first reference (which is most of the
> time because rarely do I store a list in such field), I do something like
> the following:

this shouldn't be necessary, but it would work.  well, with a couple of notes:

> def getFirstReference(object=None, reference=None):

i'm guessing that the second argument should be 'relationship' and not
'reference'.

>     return_object = None
>     if object and relationship:
>         references = object.getReferences(relationship)
>         if references:
>             if references[0]:

these nested ifs seem unnecessary.  if there aren't any references, then your
first if would fail.  if you have any references, then 'references[0]' should
exist, so the second check is superfluous.  in fact, it could cause
problems... empty containers, for instance, evaluate to False, so if you were
referencing a folder and it didn't contain any objects, you might get
unexpected behaviour.

>                 return_object = references[0]
>        
>     return return_object
>
> This seems a bit excessive to me.  Is there an easier/faster way to make
> such access?

it is a bit excessive, and, as mentioned above, it's unnecessary.  the
accessor idiom should work fine.

-r


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Plone-Users mailing list
Plone-Users@...
https://lists.sourceforge.net/lists/listinfo/plone-users
Mastro, James
Re: [Plone-Users] ReferenceField Accessor
Reply Threaded MoreMore options
Print post
Permalink
In reply to this post by Mastro, James
BTW, the signature of the method code-segment below has a typo for the
second argument.  
 
Naturally, it should be:
 
def getFirstReference(object=None, relationship=None):

Sorry for any confusion.
 
Jim

  _____  

From: plone-users-bounces@... on behalf of Mastro, James
Sent: Thu 6/29/2006 4:49 PM
To: plone-users@...
Subject: [Plone-Users] ReferenceField Accessor



I have been using ReferenceField's in my Archetype classes a lot lately.  As

I understand it (and I'm hoping that someone will tell me I'm wrong), one
can *not* simply write:
 
value = object.getMyReferenceFieldName()
 
So, when I want to access *only* the first reference (which is most of the
time because rarely do I store a list in such field), I do something like
the following:
 
def getFirstReference(object=None, reference=None):
    return_object = None
    if object and relationship:
        references = object.getReferences(relationship)
        if references:
            if references[0]:
                return_object = references[0]
       
    return return_object

This seems a bit excessive to me.  Is there an easier/faster way to make
such access?

Thanks,

Jim


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job
easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk
<http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642>
&kid=120709&bid=263057&dat=121642
_______________________________________________
Plone-Users mailing list
Plone-Users@...
https://lists.sourceforge.net/lists/listinfo/plone-users
<https://lists.sourceforge.net/lists/listinfo/plone-users>  


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Plone-Users mailing list
Plone-Users@...
https://lists.sourceforge.net/lists/listinfo/plone-users