"Tor Hildrum" <[EMAIL PROTECTED]> writes:
> Here is the code:
> --------------------------------------------
> def editItem(self, name=None, comment=None):
>
> ## Fetch the item
> try:
> item = Item.byName(name)
> except SQLObjectNotFound:
> turbogears.flash("Error!")
> redirect("/register")
>
> ## Update comment
> if comment <> None:
> item.comment = comment
>
> type = item.type
> category = type.category
>
> ## Fetch all loans for the item
> loans = Loan.selectBy(item_id=item.id)
>
> ## Order loans by the time the items was borrowed
> loans = loans.orderBy(Loan.q.borrowed)
You can specify a defaultOrder attribute on your sqlmeta class declaration:
================================================================================
class Loan(SQLObject):
class sqlmeta:
defaultOrder = 'borrowed'
...
================================================================================
You can also specify the ordering you wish while you do your select...
> ## Reverse the order
> loans = loans.reversed()
This can also be done by using
orderBy = '-borrowed'
on your select.
> for loan in loans:
>
> ## Append the loan-dictionary to my list
loan is an SQLResult, IIRC, it is definitely not a dictionary object.
> tmp.append(loan)
>
> ## Add the person who registered the loan
> tmp[i].loaner = User.get(loan.loaner.id)
You can be better with loan.loanerID instead of loan.loaner.id...
> ## If item has been delivered, Add the person who
> ## registered the loan
> if loan.deliverer == None:
> tmp[i].deliverer = None
> else:
> tmp[i].deliverer = User.get(loan.deliverer.id)
Same as above.
> ## Get the member who loaned the item
> tmp[i].member = Member.get(loan.user_id)
>
> i = i + 1
>
> ....
> --------------------------------------------
>
>
> This function takes a long time to run, and all of it can be done
> in SQL. I just don't know how using SQLObject.
When that's the case and I need more performance than I can get with the ORM I
go to the DB. I have several mappings that are mapping VIEWs instead of
tables. Those VIEWs can access functions / stored procedures and perform
other operations where I've already tweaked the SQL that I need to be used.
> I also need to get the 10 latest loans ordered by the date they where
> borrowed.
Your code orders them by reverse date... I've shown the '-borrowed' based on
your code above. If it is not that, then remove the minus signal.
> Is there any easy way to do this, because this code is killing performance.
>
> The schema looks like this:
> class Loan(SQLObject):
> class sqlmeta:
> cacheValues = False
> item_id = ForeignKey('Item')
> user_id = ForeignKey('Member')
> loaner = ForeignKey('User')
> deliverer = ForeignKey('User', default=None)
> borrowed = DateTimeCol(default=datetime.now)
> delivered = DateTimeCol(default=None)
>
> Also, for the db-gurus, is there a normalization problem in this schema?
I can't answer that without knowing the difference between a Member and a
User, but it looks OK to me on first sight...
--
Jorge Godoy <[EMAIL PROTECTED]>
-------------------------------------------------------------------------
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
_______________________________________________
sqlobject-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss