<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="content-type"> <title>Views and SQLObjects</title> <link href="layout.css" type="text/css" rel="stylesheet"> </head> <body> <div id="page"> <h1 class="doc-title"><a></a></h1> <div id="navcontainer"> <ul id="navlist"> <li class="pagenav"> <ul> <li class="page_item"> <a href="index.html" title="Project Home / Index">SQLObject</a> </li> <li class="page_item"> <a href="module-index.html" title="sqlobject package and module reference">Modules</a> </li> <li> <a href="community.html" title="Mailing List">Discuss</a> </li> <li> <a href="SQLObject.html">Documentation</a> </li> </ul> </li> </ul> </div> <hr> <div id="content"><div class="rst-doc"> <h1 class="pudge-member-page-heading">Views and SQLObjects</h1> <p>In general, if your database backend supports defining views you may define them outside of SQLObject and treat them as a regular table when defining your SQLObject class.</p> <div class="section" id="viewsqlobject"> <h1>ViewSQLObject</h1> <p>The rest of this document is experimental.</p> <p><tt class="docutils literal">from sqlobject.views import *</tt></p> <p><tt class="docutils literal">ViewSQLObject</tt> is an attempt to allow defining views that allow you to define a SQL query that acts like a SQLObject class. You define columns based on other SQLObject classes .q SQLBuilder columns, have columns that are aggregates of other columns, and join multiple SQLObject classes into one and add restrictions using SQLBuilder expressions.</p> <p>The resulting classes are currently read only, if you find use for this idea please bring discussion to the mailing list.</p> <p>A short example from the tests will suffice for now.</p> <p>Base classes:</p> <pre class="literal-block"> class PhoneNumber(SQLObject): number = StringCol() calls = SQLMultipleJoin('PhoneCall') incoming = SQLMultipleJoin('PhoneCall', joinColumn='toID') class PhoneCall(SQLObject): phoneNumber = ForeignKey('PhoneNumber') to = ForeignKey('PhoneNumber') minutes = IntCol() </pre> <p>View classes:</p> <pre class="literal-block"> class ViewPhoneCall(ViewSQLObject): class sqlmeta: idName = PhoneCall.q.id clause = PhoneCall.q.phoneNumberID==PhoneNumber.q.id minutes = IntCol(dbName=PhoneCall.q.minutes) number = StringCol(dbName=PhoneNumber.q.number) phoneNumber = ForeignKey('PhoneNumber', dbName=PhoneNumber.q.id) call = ForeignKey('PhoneCall', dbName=PhoneCall.q.id) class ViewPhone(ViewSQLObject): class sqlmeta: idName = PhoneNumber.q.id clause = PhoneCall.q.phoneNumberID==PhoneNumber.q.id minutes = IntCol(dbName=func.SUM(PhoneCall.q.minutes)) numberOfCalls = IntCol(dbName=func.COUNT(PhoneCall.q.phoneNumberID)) number = StringCol(dbName=PhoneNumber.q.number) phoneNumber = ForeignKey('PhoneNumber', dbName=PhoneNumber.q.id) calls = SQLMultipleJoin('PhoneCall', joinColumn='phoneNumberID') vCalls = SQLMultipleJoin('ViewPhoneCall', joinColumn='phoneNumberID') </pre> <a href="http://sourceforge.net/projects/sqlobject" class="reference external image-reference"><img src="http://sflogo.sourceforge.net/sflogo.php?group_id=74338&type=10" alt="Get SQLObject at SourceForge.net. Fast, secure and Free Open Source software downloads" style="width: 80px; height: 15px;" class="noborder align-center"></a> </div> </div></div> <div id="footer"> <p style="float: left;"> built with <a href="http://lesscode.org/projects/pudge/">pudge/0.1.3</a> | original design by <a href="http://blog.ratterobert.com/">ratter / robert</a> </p> <div> <br> <!-- <a name="search"> <form method="get" id="searchform" action="http://lesscode.org/blog/index.php"> <div> <input type="text" value="" name="s" id="s" /> <input type="submit" id="searchsubmit" value="Search" /> </div> </form> </a> --> <br> </div> </div> </div> </body> </html>