Would these shortcuts belong in the DAL or a plugin?

> db(logic).count()
> since you do not need the record. You just need to know if it exists.

I also need to know its ID.


On Jan 30, 1:57 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
> On Jan 28, 11:06 pm, Richard <richar...@gmail.com> wrote:
>
> > thanks that's much neater.
>
> > > does not do what you think it does.
>
> > can you explain? It worked for my test cases...
>
> You were right. It just looked wrong at a quick look. Sorry I would
> have looked in more detail.
>
> > > record = db(logic).select(table['id']).first()
>
> > I noticed you removed the ID selection part - is there any efficiency
> > gained in just selecting the ID?
>
> yes, it should be faster in jsut selecting the id. It will be even
> faster if you do
>
> db(logic).select(table['id'],limitby=(0,1)).first()
>
> or
>
> db(logic).count()
>
> since you do not need the record. You just need to know if it exists.
>
>
>
>
>
> > Richard
>
> > On Jan 29, 12:06 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > This
>
> > >     logic = None
> > >     for field, value in fields.items():
> > >         e = table[field] == value
> > >         logic = e if logic is None else logic & e
> > >     record = db(logic).select(table['id']).first()
>
> > > does not do what you think it does.
>
> > > I think you want:
>
> > >     logic=reduce(lambda a,b:a&b,[table[k]==fields[k] for k in fields])
> > >     record = db(logic).select().first()
>
> > > On Jan 28, 6:24 am, Richard <richar...@gmail.com> wrote:
>
> > > > I want to get the ID of a record with certain fields, and create it if
> > > > not already existing. Is there a shortcut for this common operation?
> > > > In Django it is called "get_or_create()"
>
> > > > Also, is there a shortcut for update_or_create()?
>
> > > > Thanks,
> > > > Richard

-- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web...@googlegroups.com.
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en.

Reply via email to