<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>The Usware Blog - Django Web Development</title>
	
	<link>http://uswaretech.com/blog</link>
	<description>Building Amazing Webapps</description>
	<lastBuildDate>Tue, 30 Aug 2011 12:22:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/uswarearticles" /><feedburner:info uri="uswarearticles" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>uswarearticles</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Moving home</title>
		<link>http://feedproxy.google.com/~r/uswarearticles/~3/wzlB6sDRqP8/</link>
		<comments>http://uswaretech.com/blog/2010/06/moving-home/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 14:59:46 +0000</pubDate>
		<dc:creator>shabda</dc:creator>
				<category><![CDATA[about]]></category>
		<category><![CDATA[meta]]></category>

		<guid isPermaLink="false">http://uswaretech.com/blog/?p=983</guid>
		<description><![CDATA[Tl;dr version: We are moving our site from Uswaretech.com to Agiliq.com. Please update your book marks and the like. Longer version: We have moved. Our new, cool, and amazing site is Agiliq.com. Built with Django, pixie dust and pink unicorns, its is sure to amaze you. Please update your bookmarks. Gory details: New url: http://agiliq.com/ [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p></p><p>Tl;dr version:</p>

<p>We are moving our site from Uswaretech.com to Agiliq.com. Please update your book marks and the like.</p>

<p>Longer version:</p>

<p>We have moved. Our new, cool, and amazing site is <a href="http://agiliq.com/">Agiliq.com</a>. Built with Django, pixie dust and pink unicorns, its is sure to amaze you. Please update your bookmarks.</p>

<p>Gory details:</p>

<ul>
<li>New url: <a href="http://agiliq.com/">http://agiliq.com/</a></li>
<li>New Blog: <a href="http://agiliq.com/blog/">http://agiliq.com/blog</a></li>
<li>Twitter: <a href="http://twitter.com/agiliqdotcom">http://twitter.com/agiliqdotcom</a></li>
<li>Github: <a href="http://github.com/agiliq">http://github.com/agiliq</a></li>
</ul>

<p>If you are client of ours, the Unfuddle and billing details would be changing soon. (You should also have received an email from us.)</p>

<ul>
<li>Unfuddle: <a href="http://agiliq.unfuddle.com/">http://agiliq.unfuddle.com/</a></li>
<li>Freshbooks: <a href="http://agiliq.freshbooks.com/">http://agiliq.freshbooks.com/</a></li>
</ul>

<p>Thanks for being with Usware/Agiliq, and we are excited to start a new pahse in our journey, and have you with us.</p>


<p>No related posts.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/uswarearticles?a=wzlB6sDRqP8:ACaJxfg5rPA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=wzlB6sDRqP8:ACaJxfg5rPA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=wzlB6sDRqP8:ACaJxfg5rPA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=wzlB6sDRqP8:ACaJxfg5rPA:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=wzlB6sDRqP8:ACaJxfg5rPA:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=wzlB6sDRqP8:ACaJxfg5rPA:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/uswarearticles/~4/wzlB6sDRqP8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://uswaretech.com/blog/2010/06/moving-home/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://uswaretech.com/blog/2010/06/moving-home/</feedburner:origLink></item>
		<item>
		<title>Great Indian Developer Summit 2010: A Review</title>
		<link>http://feedproxy.google.com/~r/uswarearticles/~3/RD5DsLsDows/</link>
		<comments>http://uswaretech.com/blog/2010/04/great-indian-developer-summit-2010-a-review/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 19:52:27 +0000</pubDate>
		<dc:creator>lakshman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://uswaretech.com/blog/?p=970</guid>
		<description><![CDATA[Great Indian Developer Summit, is the India&#8217;s largest developer conference, held at Bangalore, India; in its third edition this year. The conference concluded last Friday. The summit had about a 1000 visitors on the first day and a comparable number on the other days. The conference basically caters to the enterprises, and hence their focus [...]


Related posts:<ol><li><a href='http://uswaretech.com/blog/2009/10/pycon-india-2009/' rel='bookmark' title='Pycon India 2009 : A Review'>Pycon India 2009 : A Review</a></li>
<li><a href='http://uswaretech.com/blog/2009/11/django-for-a-rails-developer/' rel='bookmark' title='Django for a Rails Developer'>Django for a Rails Developer</a></li>
<li><a href='http://uswaretech.com/blog/2010/01/tools-of-pro-django-developer/' rel='bookmark' title='Tools of Pro Django developer &#8211; aka What powers dinette and almost every app we write.'>Tools of Pro Django developer &#8211; aka What powers dinette and almost every app we write.</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p><img src="http://i.imgur.com/38pUQ.jpg" alt="GIDS_logo" /></p>

<p><a href="http://devmarch.com/developersummit/">Great Indian Developer Summit</a>, is the India&#8217;s largest developer conference, held at Bangalore, India; in its third edition this year. The conference concluded last Friday. The summit had about a 1000 visitors on the first day and a comparable number on the other days.</p>

<p>The conference basically caters to the enterprises, and hence their focus on the .Net, Java and Flash. There were many <a href="http://www.developermarch.com/developersummit/speakers.html">star speakers</a>.</p>

<p><img src="http://i.imgur.com/o9f3m.jpg" alt="I'm speaking" /></p>

<p>I gave a talk on django, introducing it and then explaining the standard community conventions to make good reusable applications. Django, being a social software, developed by a community, it is important to cover the community aspect, and the innumerable number of reusable application that exist. So, there is enough of that.</p>

<div style="width:425px" id="__ss_3738671"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/scorpion032/building-pluggable-web-applications-using-django" title="Building Pluggable Web Applications using Django">Building Pluggable Web Applications using Django</a></strong><object width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=contents-100415143355-phpapp01&#038;stripped_title=building-pluggable-web-applications-using-django" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=contents-100415143355-phpapp01&#038;stripped_title=building-pluggable-web-applications-using-django" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object><div style="padding:5px 0 12px">View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/scorpion032">scorpion032</a>.</div></div>

<p>The slides themselves were created using <a href="http://bitbucket.org/rivanvx/beamer/wiki/Home">Beamer class</a>, in latex. The <a href="http://github.com/becomingGuru/gids-django-ppt">code is on Github</a>, and there are some custom commands. How the beamer class that was created specifically to create bullet point presentations can so totally create web2.0 kind of slides with images and text on them, by just a simple command, and continues to be not only relevant, but also best in class presentation creator, gives me a kick.  I always hated PowerPoint and could have in no way done so many slides using it, that way. &#8220;Write the relevant text and give the path of the image to include&#8221;, or &#8220;give the path of the file that has code in it, to include in this slide&#8221; is the way I think and slide maker should create slides from. So glad beamer and latex could do that. It should be relatively easy to write a  markdown to ppt compiler, given the presentation format. Until next time.</p>

<p><img src="http://i.imgur.com/6YKkH.jpg" alt="Banner" /></p>

<p>I am no expert in .Net and given an option to, would avoid working with Java. Nevertheless, the sessions by <a href="http://www.developermarch.com/developersummit/speakers.html#Venkat_Subramaniam">Venkat Subramaniam</a> were interesting. It was a good experience to meet many great developers and people. <a href="http://www.developermarch.com/developersummit/speakers.html#Matthew_McCullough">Matthew_McCullough</a> is an expert in large number of things. Session by <a href="http://www.developermarch.com/developersummit/speakers.html#Scott_Davis">Scott Davis</a> was definitely awesome. It was overall a great experience.</p>


<p>Related posts:<ol><li><a href='http://uswaretech.com/blog/2009/10/pycon-india-2009/' rel='bookmark' title='Pycon India 2009 : A Review'>Pycon India 2009 : A Review</a></li>
<li><a href='http://uswaretech.com/blog/2009/11/django-for-a-rails-developer/' rel='bookmark' title='Django for a Rails Developer'>Django for a Rails Developer</a></li>
<li><a href='http://uswaretech.com/blog/2010/01/tools-of-pro-django-developer/' rel='bookmark' title='Tools of Pro Django developer &#8211; aka What powers dinette and almost every app we write.'>Tools of Pro Django developer &#8211; aka What powers dinette and almost every app we write.</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/uswarearticles?a=RD5DsLsDows:uMhzMeVhueA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=RD5DsLsDows:uMhzMeVhueA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=RD5DsLsDows:uMhzMeVhueA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=RD5DsLsDows:uMhzMeVhueA:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=RD5DsLsDows:uMhzMeVhueA:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=RD5DsLsDows:uMhzMeVhueA:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/uswarearticles/~4/RD5DsLsDows" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://uswaretech.com/blog/2010/04/great-indian-developer-summit-2010-a-review/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://uswaretech.com/blog/2010/04/great-indian-developer-summit-2010-a-review/</feedburner:origLink></item>
		<item>
		<title>CSS Frameworks</title>
		<link>http://feedproxy.google.com/~r/uswarearticles/~3/pbwlk4CGAD4/</link>
		<comments>http://uswaretech.com/blog/2010/03/css-frameworks/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 09:27:23 +0000</pubDate>
		<dc:creator>saikiran</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Design]]></category>

		<guid isPermaLink="false">http://uswaretech.com/blog/?p=957</guid>
		<description><![CDATA[A framework is a basic conceptual structure which you can use as a “scratch” for your web-projects. For instance, instead of defining global reset, consistent baseline, typographic rules or basic styles for forms over and over again — every time you work on a new project — you can prepare a default-style once and reuse [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p></p><p>A framework is a basic conceptual structure which you can use as a “scratch” for your web-projects. For instance, instead of defining global reset, consistent baseline, typographic rules or basic styles for forms over and over again — every time you work on a new project — you can prepare a default-style once and reuse it in all your future projects. This is what you call a CSS Framework.
<strong> </strong></p>

<p><strong>Major Advantages of CSS Frameworks</strong></p>

<ul>
    <li>Increase productivity.</li>
    <li>Cross Bowser Compatibility</li>
    <li>Clean, Well structured easy to maintain code base.</li>
</ul>

<p><strong>Disadvantages of CSS Frameworks</strong></p>

<ul>
    <li>Takes time to understand the CSS framework.</li>
    <li>You might inherit someone’s bugs or mistakes.</li>
    <li>You get a bloated source code.</li>
</ul>

<p>Among most of the CSS Frameworks that i have come across Blueprint &amp; YUI frameworks seems to be the most powerful frameworks.</p>

<p><strong>Blueprint CSS Framework</strong>
Blueprint is a CSS framework, which aims to cut down on your development time.
<a title="BluePrint CSS Grid Generator" href="http://bgg.kematzy.com/" target="_blank">Blueprint Grid CSS Generator</a> helps you generate more flexible versions of Blueprint&#8217;s grid.css and compressed.css and grid.png files. Whether you prefer 8, 10,16 or 24 columns in your design, this generator now enables you that flexibility with Blueprint.</p>

<p><strong>
Blueprint CSS Framework provides</strong></p>

<ul>
    <li>reset.css , CSS reset for default browser styles</li>
    <li>print.css ,  A stylesheet for printing</li>
    <li>typography.css,  For basic typographic rules,</li>
    <li>grid.css ,  For grid-based layouts or</li>
    <li>form.css for basic form styling</li>
    <li>Blue Print also provides psd file for basic understanding of grid based layout.</li>
</ul>

<p><strong>YUI CSS Framework</strong>
One of the major advantages with YUI is it supports fluid-width (100%) layouts as well as preset fixed-width layouts at 750px, 950px, and 974px, and the ability to easily customize to any number and the documentation is very clear.
The <a href="http://developer.yahoo.com/yui/grids/builder/" target="_blank">YUI Grids Builder</a> — a simple interface for Grids customization.
Photoshop template for use with YUI is not available.If it is provided it will be a great help for newbees to understand the system.</p>

<p><strong>
YUI CSS Framework provides</strong></p>

<ul>
    <li>reset.css , CSS reset for default browser styles</li>
    <li>fonts.css,  For basic typographic rules,</li>
    <li>grid.css ,  For grid-based layouts or</li>
</ul>

<div id="_mcePaste"><strong>960 grid system</strong></div>

<p>Custom CSS Generator works fine and you can chnage the number of columns and get the css code.Based on your templates you can customise it accordingly.Html layout generator is quite confusing, while I tried to use it.</p>

<p><strong>Yaml Builder</strong></p>

<p>Yaml is having a wonderful  tool to make custom grid structure and the css is easy to understand and customise it. Newbees can work with this tool easily.We can create a grid of any size and we can mention the number of columns etc&#8230;DOM Tree is an interesting option in YAML builder, it allows you to easily recognise the divs , if you click on the code it shows that perticular div in marked in green color on the design.</p>

<p>Eventhough Blueprint and YUI have advantages and disadvantages, Blueprint seems to be quite comfortable for me to work with  and in terms of flexibility as well.</p>


<p>No related posts.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/uswarearticles?a=pbwlk4CGAD4:2JTly2iVfoA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=pbwlk4CGAD4:2JTly2iVfoA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=pbwlk4CGAD4:2JTly2iVfoA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=pbwlk4CGAD4:2JTly2iVfoA:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=pbwlk4CGAD4:2JTly2iVfoA:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=pbwlk4CGAD4:2JTly2iVfoA:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/uswarearticles/~4/pbwlk4CGAD4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://uswaretech.com/blog/2010/03/css-frameworks/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://uswaretech.com/blog/2010/03/css-frameworks/</feedburner:origLink></item>
		<item>
		<title>Rails and Django commands : comparison  and conversion</title>
		<link>http://feedproxy.google.com/~r/uswarearticles/~3/Hu5kSlBWUeE/</link>
		<comments>http://uswaretech.com/blog/2010/03/rails-and-django-commands-comparison-and-conversion/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 13:05:30 +0000</pubDate>
		<dc:creator>shabda</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://uswaretech.com/blog/?p=936</guid>
		<description><![CDATA[The most commonly used Rails commands and their Django equivalents Rails Django rails console manage.py shell rails server manage.py runserver rake None rails generate None rails dbconsole manage.py dbshell rails app_name django-admin.py startproject/manage.py startapp rake db:create manage.py syncdb The salient points to note are, Django has all commands via manage.py, Rails has it broken into [...]


Related posts:<ol><li><a href='http://uswaretech.com/blog/2009/11/django-for-a-rails-developer/' rel='bookmark' title='Django for a Rails Developer'>Django for a Rails Developer</a></li>
<li><a href='http://uswaretech.com/blog/2010/03/the-rails-and-django-models-layer-rosseta-stone/' rel='bookmark' title='The Rails and Django models layer Rosseta stone'>The Rails and Django models layer Rosseta stone</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p>The most commonly used Rails commands and their Django equivalents</p>

<table>
<thead>
<tr>
  <th>Rails</th>
  <th>Django</th>
</tr>
</thead>
<tbody>
<tr>
  <td>rails console</td>
  <td>manage.py shell</td>
</tr>
<tr>
  <td>rails server</td>
  <td>manage.py runserver</td>
</tr>
<tr>
  <td>rake</td>
  <td>None</td>
</tr>
<tr>
  <td>rails generate</td>
  <td>None</td>
</tr>
<tr>
  <td>rails dbconsole</td>
  <td>manage.py dbshell</td>
</tr>
<tr>
  <td>rails app_name</td>
  <td>django-admin.py startproject/manage.py startapp</td>
</tr>
<tr>
  <td>rake db:create</td>
  <td>manage.py syncdb</td>
</tr>
</tbody>
</table>

<p>The salient points to note are,</p>

<ol>
<li>Django has all commands via <code>manage.py</code>, Rails has it broken into <code>rails</code> and <code>rake</code>.</li>
<li>Overall there are more Rails+Rake commands available than Django commands</li>
<li>There is no one to one mapping between Rails and Django commands.
Eg. There are no equivalent to rake doc:* or rake notes in Django.</li>
</ol>

<p>Similarly there is no equivalent to <code>manage.py createsuperuser</code> in rails.</p>

<h4>References</h4>

<p><a href="http://docs.djangoproject.com/en/dev/ref/django-admin/">http://docs.djangoproject.com/en/dev/ref/django-admin/</a>
<a href="http://guides.rails.info/command_line.html">http://guides.rails.info/command_line.html</a>
<a href="http://github.com/uswaretech/Acts-as-Django/blob/master/commands.markdown">http://github.com/uswaretech/Acts-as-Django/blob/master/commands.markdown</a></p>


<p>Related posts:<ol><li><a href='http://uswaretech.com/blog/2009/11/django-for-a-rails-developer/' rel='bookmark' title='Django for a Rails Developer'>Django for a Rails Developer</a></li>
<li><a href='http://uswaretech.com/blog/2010/03/the-rails-and-django-models-layer-rosseta-stone/' rel='bookmark' title='The Rails and Django models layer Rosseta stone'>The Rails and Django models layer Rosseta stone</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/uswarearticles?a=Hu5kSlBWUeE:p1Wfn32XYh0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=Hu5kSlBWUeE:p1Wfn32XYh0:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=Hu5kSlBWUeE:p1Wfn32XYh0:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=Hu5kSlBWUeE:p1Wfn32XYh0:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=Hu5kSlBWUeE:p1Wfn32XYh0:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=Hu5kSlBWUeE:p1Wfn32XYh0:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/uswarearticles/~4/Hu5kSlBWUeE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://uswaretech.com/blog/2010/03/rails-and-django-commands-comparison-and-conversion/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		<feedburner:origLink>http://uswaretech.com/blog/2010/03/rails-and-django-commands-comparison-and-conversion/</feedburner:origLink></item>
		<item>
		<title>The Rails and Django models layer Rosseta stone</title>
		<link>http://feedproxy.google.com/~r/uswarearticles/~3/TDUlfAqIu_4/</link>
		<comments>http://uswaretech.com/blog/2010/03/the-rails-and-django-models-layer-rosseta-stone/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 10:42:50 +0000</pubDate>
		<dc:creator>shabda</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://uswaretech.com/blog/?p=928</guid>
		<description><![CDATA[Rails Active records and Django models are more similar than they are different. This is a quick guide to converting between Rails 3 and Django 1.2, and is available on github at http://github.com/uswaretech/Acts-as-Django Defining models Both Django and Rails keep the canonical database representation in ruby or python. #Django class Post(models.Model): name = models.CharField(max_length = [...]


Related posts:<ol><li><a href='http://uswaretech.com/blog/2010/01/django-models-tutorial/' rel='bookmark' title='Doing things with Django models &#8211; aka &#8211; Django models tutorial'>Doing things with Django models &#8211; aka &#8211; Django models tutorial</a></li>
<li><a href='http://uswaretech.com/blog/2010/03/rails-and-django-commands-comparison-and-conversion/' rel='bookmark' title='Rails and Django commands : comparison  and conversion'>Rails and Django commands : comparison  and conversion</a></li>
<li><a href='http://uswaretech.com/blog/2009/11/django-for-a-rails-developer/' rel='bookmark' title='Django for a Rails Developer'>Django for a Rails Developer</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p>Rails Active records and Django models are more similar than they are different. This is a quick guide to converting between Rails 3 and Django 1.2, and is available on github at <a href="http://github.com/uswaretech/Acts-as-Django">http://github.com/uswaretech/Acts-as-Django</a></p>

<h4>Defining models</h4>

<p>Both Django and Rails keep the canonical database representation in ruby or python.</p>

<pre><code>#Django

class Post(models.Model):
    name = models.CharField(max_length = 100, )
    slug = models.CharField(max_length = 100, )
    body = models.TextField()

class Comments(models.Model):
    post = models.ForeignKey(Post)
    username = models.CharField(max_length = 100, )
    comment = models.TextField()



#Rails

#db/schema.rb
ActiveRecord::Schema.define(:version =&gt; 20100319195739) do

  create_table "comments", :force =&gt; true do |t|
    t.string   "username"
    t.text     "comment"
    t.integer  "post_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "posts", :force =&gt; true do |t|
    t.string   "name"
    t.string   "slug"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

#In apps models
class Post &lt; ActiveRecord::Base
  has_many :comments
end

class Comment &lt; ActiveRecord::Base
  belongs_to :post
end
</code></pre>

<p>The main difference is that Django keeps it one file, while rails has it split over many files. In Django, the foreign key is specified only on the Child model
via <code>models.ForeignKey</code> but in Rails both sides of relationship need to be specified via <code>has_many</code> and <code>belongs_to</code>.</p>

<h4>Create a object without saving it</h4>

<pre><code>In [1]: from blog.models import *

In [2]: post = Post()

In [3]: print post.id
None



irb(main):001:0&gt; post = Post.new
=&gt; #&lt;Post id: nil, name: nil, slug: nil, body: nil, created_at: nil, updated_at: nil&gt;
irb(main):002:0&gt; post.id
=&gt; nil
</code></pre>

<h4>Set values and save to the database</h4>

<pre><code>In [4]: post.name = "Hello"

In [5]: post.slug = "hello"

In [6]: post.body = "Hello, this is a post"

In [8]: post.save()

In [9]: post.id
Out[9]: 1


irb(main):001:0&gt; post = Post.new
=&gt; #&lt;Post id: nil, name: nil, slug: nil, body: nil, created_at: nil, updated_at: nil&gt;
irb(main):002:0&gt; post.id
=&gt; nil
irb(main):003:0&gt; post.name = "Hello"
=&gt; "Hello"
irb(main):004:0&gt; post.slug = "hello"
=&gt; "hello"
irb(main):005:0&gt;  post.body = "Hello, this is a post"
=&gt; "Hello, this is a post"
irb(main):006:0&gt; post.save()
=&gt; true
irb(main):007:0&gt; post.id
=&gt; 2
</code></pre>

<p>There is not much to see here, both DJango and Rails create the object in essentially the same way. Until you call save, the objects are not saved to the database.</p>

<h4>Find object by the primary key.</h4>

<pre><code>In [10]: Post.objects.get(id = 1)
Out[10]: &lt;Post: Post object&gt;

irb(main):008:0&gt; pst = Post.find(2)
=&gt; #&lt;Post id: 2, name: "Hello", slug: "hello", body: "Hello, this is a post", created_at: "2010-03-19 20:11:34", updated_at: "2010-03-19 20:11:34"&gt;
</code></pre>

<p>What Rails calls <code>find</code>, Django calls <code>get</code>.</p>

<h4>More create methods</h4>

<pre><code>In [12]: Post.objects.create(name="Hi", slug="hi", body="Hi hi hi.")
Out[12]: &lt;Post: Post object&gt;


irb(main):012:0&gt; Post.create(:name=&gt;"Hi", :slug=&gt;"Hi", :body=&gt;"Hi hi hi")
=&gt; #&lt;Post id: 4, name: "Hi", slug: "Hi", body: "Hi hi hi", created_at: "2010-03-19 20:17:31", updated_at: "2010-03-19 20:17:31"&gt;


In [14]: Post.objects.get_or_create(name="Hi")
Out[14]: (&lt;Post: Post object&gt;, False)

irb(main):015:0&gt; Post.find_or_create_by_name("Hi")
=&gt; #&lt;Post id: 4, name: "Hi", slug: "Hi", body: "Hi hi hi", created_at: "2010-03-19 20:17:31", updated_at: "2010-03-19 20:17:31"&gt;
</code></pre>

<p>You can create in one step without calling <code>save</code> via a <code>create</code> in both Rails and Django.
While rails has dynamically named methods, Django accepts named parameters in <code>filter</code> and <code>get_or_create</code>.</p>

<h4>More find methods</h4>

<pre><code>In [15]: Post.objects.filter(id__in = [1, 2, 3])
Out[15]: [&lt;Post: Post object&gt;, &lt;Post: Post object&gt;]

irb(main):017:0&gt; Post.find(1, 2, 3)
=&gt; [#&lt;Post id: 1, name: nil, slug: nil, body: nil, created_at: "2010-03-19 19:58:31", updated_at: "2010-03-19 19:58:31"&gt;, #&lt;Post id: 2, name: "Hello", slug: "hello", body: "Hello, this is a post", created_at: "2010-03-19 20:11:34", updated_at: "2010-03-19 20:11:34"&gt;, #&lt;Post id: 3, name: nil, slug: nil, body: nil, created_at: "2010-03-19 20:16:04", updated_at: "2010-03-19 20:16:04"&gt;]

In [16]: Post.objects.get(name="Hi")
Out[16]: &lt;Post: Post object&gt;


irb(main):023:0&gt; Post.find_by_name("Hi")
=&gt; #&lt;Post id: 4, name: "Hi", slug: "Hi", body: "Hi hi hi", created_at: "2010-03-19 20:17:31", updated_at: "2010-03-19 20:17:31"&gt;


In [17]: Post.objects.all()
Out[17]: [&lt;Post: Post object&gt;, &lt;Post: Post object&gt;]


irb(main):024:0&gt; Post.find(:all)
=&gt; [#&lt;Post id: 1, name: nil, slug: nil, body: nil, created_at: "2010-03-19 19:58:31", updated_at: "2010-03-19 19:58:31"&gt;, #&lt;Post id: 2, name: "Hello", slug: "hello", body: "Hello, this is a post", created_at: "2010-03-19 20:11:34", updated_at: "2010-03-19 20:11:34"&gt;, #&lt;Post id: 3, name: nil, slug: nil, body: nil, created_at: "2010-03-19 20:16:04", updated_at: "2010-03-19 20:16:04"&gt;, #&lt;Post id: 4, name: "Hi", slug: "Hi", body: "Hi hi hi", created_at: "2010-03-19 20:17:31", updated_at: "2010-03-19 20:17:31"&gt;]

In [18]: pp = Post.objects.filter(name="Hi")

In [19]: pp
Out[19]: [&lt;Post: Post object&gt;]
In [20]: pp[0]
Out[20]: &lt;Post: Post object&gt;


irb(main):032:0&gt; pp = Post.where({:name=&gt;"Hi"})
=&gt; #&lt;ActiveRecord::Relation:0xb6aedc04 @arel=nil, @select_values=[], @last=nil, @order_values=[], @group_values=[], ...
irb(main):033:0&gt; pp[0]
=&gt; #&lt;Post id: 4, name: "Hi", slug: "Hi", body: "Hi hi hi", created_at: "2010-03-19 20:17:31", updated_at: "2010-03-19 20:17:31"&gt;

In [21]: Post.objects.filter(name__in = ["Hi", "Hello"])
Out[21]: [&lt;Post: Post object&gt;, &lt;Post: Post object&gt;]

irb(main):034:0&gt; pp = Post.where({:name=&gt;["Hi", "Hello"]})
=&gt; #&lt;ActiveRecord::Relation:0xb6ae4c6c @arel=nil, @select_values=
....
irb(main):035:0&gt; pp[0]
=&gt; #&lt;Post id: 2, name: "Hello", slug: "hello", body: "Hello, this is a post", created_at: "2010-03-19 20:11:34", updated_at: "2010-03-19 20:11:34"&gt;
irb(main):036:0&gt; pp[1]
=&gt; #&lt;Post id: 4, name: "Hi", slug: "Hi", body: "Hi hi hi", created_at: "2010-03-19 20:17:31", updated_at: "2010-03-19 20:17:31"&gt;


In [29]: p = Post.objects.filter(name__in = ["Hi", "Hello"]).only("name")

irb(main):041:0&gt; pp = Post.where({:name=&gt;["Hi", "Hello"]}).select("name")
</code></pre>

<p>The query capability and syntax are similar with major differences being,</p>

<ol>
<li>Rails has dynamically named finders, while Django accepts keyword arguments for same.</li>
<li>SQL operators like <code>in</code> are fired by <code>__</code> in Django, while Rails infers it based on the data type.</li>
</ol>

<h4>Limit, offset and order</h4>

<pre><code>In [30]: p = Post.objects.filter(name__in = ["Hi", "Hello"])[:1]

In [31]: p
Out[31]: [&lt;Post: Post object&gt;]

pp = Post.where({:name=&gt;["Hi", "Hello"]}).select("name").limit(1)

irb(main):049:0&gt; pp[0]
=&gt; #&lt;Post name: "Hello"&gt;
irb(main):050:0&gt; pp[1]
=&gt; nil

=&gt; #&lt;Post id: 4, name: "Hi", slug: "Hi", body: "Hi hi hi", created_at: "2010-03-19 20:17:31", updated_at: "2010-03-19 20:17:31"&gt;
irb(main):067:0&gt; pp = Post.where({:name=&gt;["Hi", "Hello"]}).limit(1).offset(1)

irb(main):067:0&gt; pp[0]
=&gt; #&lt;Post id: 4, name: "Hi", slug: "Hi", body: "Hi hi hi", created_at: "2010-03-19 20:17:31", updated_at: "2010-03-19 20:17:31"&gt;
irb(main):068:0&gt; pp[1]
=&gt; nil

In [34]: p = Post.objects.filter(name__in = ["Hi", "Hello"])[1:2]

In [35]: p[0].name
Out[35]: u'Hi'

In [36]: p[1].name
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)

/home/shabda/Code/Django/Weblog/&lt;ipython console&gt; in &lt;module&gt;()

/usr/local/lib/python2.6/dist-packages/django/db/models/query.py in __getitem__(self, k)
    185             qs = self._clone()
    186             qs.query.set_limits(k, k + 1)
--&gt; 187             return list(qs)[0]
    188         except self.model.DoesNotExist, e:
    189             raise IndexError(e.args)

IndexError: list index out of range


In [39]: p = Post.objects.order_by("name")[1:2]

In [40]: p
Out[40]: [&lt;Post: Post object&gt;]


irb(main):069:0&gt; pp = Post.order("name").limit(1).offset(1)
</code></pre>

<p>Again fairly similar, both Django and rails provide ordering, offset and limits, while Django does
this via array slicing, Rails does this via functions. Also both Django and Rails allow method chaining, and Sql is only
evaluated lazily when needed.</p>

<h4>Specifying Model associations.</h4>

<p>Django: <code>models.ForeignKey</code><br />
Rails: <code>belongs_to</code></p>

<p>Django: <code>models.ManyToManyField</code><br />
Rails: <code>has_and_belongs_to_many</code></p>

<p>Django: <code>models.OneToOneField</code><br />
Rails: <code>has_one</code></p>

<p>Django: No needed<br />
Rails: <code>has_many</code></p>

<p>Again Rails and Django are similar with Django automatically inferring the reverse relationships.</p>

<h4>Polymorphic Associations and Generic Relations</h4>

<p>You possibly have pictures in your application which can be attached to any object. Both Django and Rails provide options for it via
Polymorphic Associations and Generic Relations</p>

<pre><code>class Picture(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()

class Article(models.Model):
    body = models.TextField()
    picture =  generic.GenericRelation(Picture)

class BioGraphy(models.Model):
    bio  = models.TextField()
    picture = generic.GenericRelation(Picture)



class Picture &lt; ActiveRecord::Base
  belongs_to :imageable, :polymorphic =&gt; true
end

class Employee &lt; ActiveRecord::Base
  has_many :pictures, :as =&gt; :imageable
end

class Product &lt; ActiveRecord::Base
  has_many :pictures, :as =&gt; :imageable
end
</code></pre>

<h4>Self Joins</h4>

<p>Self joins are a special case of Foreign key where a Object has a relationship to itself. Both Django and Rails handle it normally via their FK mechanisms</p>

<pre><code>class Employee(models.Model):
    manager = models.ForeignKey("self", related_name = "subordinates")


class Employee &lt; ActiveRecord::Base
  has_many :subordinates, :class_name =&gt; "Employee",
    :foreign_key =&gt; "manager_id"
  belongs_to :manager, :class_name =&gt; "Employee"
end
</code></pre>

<h2>References</h2>

<p><a href="http://guides.rails.info/">guides.rails.info</a>
<a href="http://djangoproject.com/documentation/">djangoproject.com/documentation/</a></p>


<p>Related posts:<ol><li><a href='http://uswaretech.com/blog/2010/01/django-models-tutorial/' rel='bookmark' title='Doing things with Django models &#8211; aka &#8211; Django models tutorial'>Doing things with Django models &#8211; aka &#8211; Django models tutorial</a></li>
<li><a href='http://uswaretech.com/blog/2010/03/rails-and-django-commands-comparison-and-conversion/' rel='bookmark' title='Rails and Django commands : comparison  and conversion'>Rails and Django commands : comparison  and conversion</a></li>
<li><a href='http://uswaretech.com/blog/2009/11/django-for-a-rails-developer/' rel='bookmark' title='Django for a Rails Developer'>Django for a Rails Developer</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/uswarearticles?a=TDUlfAqIu_4:ZL6d_KisQ5g:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=TDUlfAqIu_4:ZL6d_KisQ5g:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=TDUlfAqIu_4:ZL6d_KisQ5g:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=TDUlfAqIu_4:ZL6d_KisQ5g:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=TDUlfAqIu_4:ZL6d_KisQ5g:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=TDUlfAqIu_4:ZL6d_KisQ5g:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/uswarearticles/~4/TDUlfAqIu_4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://uswaretech.com/blog/2010/03/the-rails-and-django-models-layer-rosseta-stone/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		<feedburner:origLink>http://uswaretech.com/blog/2010/03/the-rails-and-django-models-layer-rosseta-stone/</feedburner:origLink></item>
		<item>
		<title>Doing things with Django models – aka – Django models tutorial</title>
		<link>http://feedproxy.google.com/~r/uswarearticles/~3/kyeyRwQmu_c/</link>
		<comments>http://uswaretech.com/blog/2010/01/django-models-tutorial/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 05:00:17 +0000</pubDate>
		<dc:creator>shabda</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://uswaretech.com/blog/?p=891</guid>
		<description><![CDATA[Django abstracts most of the actions you would be doing with the Database. What it doesn&#8217;t abstracts, and doesn&#8217;t try to abstract is the Database modelling part. This is a quick tutorial describing to how model your data in Django models.py, and how to access and modify them. Consider a hypothetical HR department, which wants [...]


Related posts:<ol><li><a href='http://uswaretech.com/blog/2009/08/django-aggregation-tutorial/' rel='bookmark' title='Django aggregation tutorial'>Django aggregation tutorial</a></li>
<li><a href='http://uswaretech.com/blog/2010/03/the-rails-and-django-models-layer-rosseta-stone/' rel='bookmark' title='The Rails and Django models layer Rosseta stone'>The Rails and Django models layer Rosseta stone</a></li>
<li><a href='http://uswaretech.com/blog/2008/10/dynamic-forms-with-django/' rel='bookmark' title='Dynamic forms with Django'>Dynamic forms with Django</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p>Django abstracts most of the actions you would be doing with the Database.
What it doesn&#8217;t abstracts, and doesn&#8217;t try to abstract is the Database modelling part. This is a quick tutorial
describing to how model your data in Django models.py, and how to access and modify them.</p>

<p>Consider a hypothetical HR department, which wants you to build an application to track and manage their processes.
They have employees who work for a department, contractors who work for multiple department. Let&#8217;s see how you
you would do that in Django.</p>

<pre><code>from django.db import models

class Employee(models.Model):
    name = models.CharField(max_length = 100)
    department = models.ForeignKey("Department")

class Department(models.Model):
    name = models.CharField(max_length = 100)

class EmployeeHistory(models.Model):
    employee = models.OneToOneField(Employee)
    date_joined = models.DateField()
    marital_status = models.BooleanField()

class Contactor(models.Model):
    name = models.CharField(max_length = 100)
    departments = models.ManyToManyField(Department)
</code></pre>

<p>Let&#8217;s see the type of relationship we created here.</p>

<p>An <code>Employee</code> has a Many-to-one relationship with <code>Department</code>, (i.e. One department will have many
<code>Employee</code>, but one employee will have a single departments.)</p>

<p>So <code>Employee</code> has a field <code>department = models.ForeignKey("Department")</code>. See that the <code>ForeignKey</code> field
was added on the class which has in <em>many</em>. In database, this creates a FK from in <code>Employee</code> table which refernces
<code>Departments</code>.</p>

<p>A <code>Contractor</code> has many-to-many relationships with <code>Department</code>, so it has a <code>ManyToMany</code> field. This field can be created
on either classes. At a database level this creates a new table which has a FK to both the tables.</p>

<p>A <code>Employee</code> has one-to-one relationship with <code>EmployeeHistory</code>. The thing to note here is that whatever we could do with
OneToOneField, we can do by including the fields in the Other Class directly. However when there are fields which are only rarely
needed with a given model, it is useful to separate them via a one-to-one field. The one to one field can be on either class.</p>

<h4>Accessing objects.</h4>

<h5>Getting a specific employee.</h5>

<pre><code>Employee.objects.get(pk = someval)
Employee.objects.get(name= someval)
</code></pre>

<h5>Given an department get all employees.</h5>

<pre><code>department.employee_set.all()
</code></pre>

<p>The <code>department</code> gets an attribute name <code>&lt;FKClass&gt;_set</code>.</p>

<p>Given an employee, get all colleagues.</p>

<pre><code>employee.department.employee_set.objects.all()
</code></pre>

<p>To get siblings, get parents, and get all children.</p>

<p>Given an department, get number of employees.</p>

<pre><code>department.employee_set.all().count()
</code></pre>

<p>Ok so the HR department bosses are happy with what they see, and ask you to track this data,
who is the manager of each employee, who is the HOD of each department and when did each employee took leave.</p>

<pre><code>from django.db import models

class Employee(models.Model):
    name = models.CharField(max_length = 100)
    manager = models.ForeignKey("self", blank = True, null = True)
    department = models.ForeignKey("Department")

class Department(models.Model):
    hod = models.ForeignKey("Employee")
    name = models.CharField(max_length = 100)

class EmployeeHistory(models.Model):
    employee = models.OneToOneField(Employee)
    date_joined = models.DateField()
    marital_status = models.BooleanField()

class Contactors(models.Model):
    name = models.CharField(max_length = 100)
    departments = models.ManyToManyField(Department)


class EmployeeLeave(models.Model):
    leave_taken = models.DateField()
    employee = models.ForeignKey(Employee)
</code></pre>

<p>So we now have new fields <code>hod = models.OneToOneField("Employee")</code> in department and
<code>manager = models.ForeignKey("self", blank = True, null = True)</code> and a new model <code>EmployeeLeave</code>. Let su see
the new realtions,</p>

<p>As one <code>Department</code> will have one <code>Employee</code> as hod, and <code>Employee</code> can head at max one <code>Department</code>, we have a one to one relationship<br />
As many <code>Employee</code> will report to another <code>Employee</code>, so we have a FK on <code>Employee</code>, referencing <code>self</code>.
As <code>Employee</code> will take many <code>EmployeeLeave</code>, <code>EmployeeLeave</code> has a FK to <code>Epployee</code></p>

<p>Let us see some queries.</p>

<p>Get all leaves taken by an employee this year</p>

<pre><code>import datetime
today = datetime.date.today()
start_of_year = datetime.datetime(today.year, 1, 1)
leaves_taken = employee.employeeleave_set.filter(leave_taken__gt=start_of_year, leave_taken__lt = today)
</code></pre>

<p>Get a list of all HODs.</p>

<pre><code>#As there are going to be as many Employees as departments
departments = Department.objects.all()
employees = [department.hod for department in departments]
</code></pre>

<p>Get a list of all departments a contractor works for.</p>

<pre><code>contractor.department_set.all()
</code></pre>

<p>Get a list of all contractor who work for a department.</p>

<pre><code>department.contractor_set.all()
</code></pre>

<p>Note that each side of a many to many relationship get a Manager.</p>

<p>List of all managers in a given department.</p>

<pre><code>Todo#(Can't think of any one query way to do this. If you know, let me know <img src='http://uswaretech.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )
</code></pre>

<p>List of all leaves taken in a given department.</p>

<pre><code>EmployeeLeave.objects.filter(employee__department = department)
</code></pre>

<p>None that we used double underscores <code>__</code> to do a filtering on a field across Entities.</p>

<p>List of all employees which joined a given department this year.</p>

<pre><code>Employee.objects.filter(department = department, employment_history__date_joined__gte=start_of_year, )
</code></pre>

<p>Note that we used a double underscore <code>__</code> twice, first to go across entities, and then to define the type of filter(<code>__gte</code>).
Also we specified two filter conditions, so they were <code>ANDed</code> together.</p>

<p>List all employees which either report too a given employee, or joined before him.</p>

<pre><code>Employee.objects.filter(Q(manager = employee)|Q(employee_history__date_joined__lt = employee.employee_history.date_joined))
</code></pre>

<p>Note the new construct <code>Q</code>, they are used to specify complex boolean operations. Here we used the <code>|</code> (or operator) to specify or condition.</p>

<hr />

<p><a href="http://feeds.feedburner.com/uswarearticles">Do you subscribe to our rss feed</a>? The new feed has full text feed and many other goodness. So make sure you are subscribed to the new feed with extra fortified vitamins. <a href="http://feeds.feedburner.com/uswarearticles">Subscribe now</a>.</p>


<p>Related posts:<ol><li><a href='http://uswaretech.com/blog/2009/08/django-aggregation-tutorial/' rel='bookmark' title='Django aggregation tutorial'>Django aggregation tutorial</a></li>
<li><a href='http://uswaretech.com/blog/2010/03/the-rails-and-django-models-layer-rosseta-stone/' rel='bookmark' title='The Rails and Django models layer Rosseta stone'>The Rails and Django models layer Rosseta stone</a></li>
<li><a href='http://uswaretech.com/blog/2008/10/dynamic-forms-with-django/' rel='bookmark' title='Dynamic forms with Django'>Dynamic forms with Django</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/uswarearticles?a=kyeyRwQmu_c:owB5n1HVeHM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=kyeyRwQmu_c:owB5n1HVeHM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=kyeyRwQmu_c:owB5n1HVeHM:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=kyeyRwQmu_c:owB5n1HVeHM:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=kyeyRwQmu_c:owB5n1HVeHM:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=kyeyRwQmu_c:owB5n1HVeHM:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/uswarearticles/~4/kyeyRwQmu_c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://uswaretech.com/blog/2010/01/django-models-tutorial/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		<feedburner:origLink>http://uswaretech.com/blog/2010/01/django-models-tutorial/</feedburner:origLink></item>
		<item>
		<title>WordPress and Django: best buddies</title>
		<link>http://feedproxy.google.com/~r/uswarearticles/~3/oIyNNJU6mL0/</link>
		<comments>http://uswaretech.com/blog/2010/01/wordpress-and-django-best-buddies/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 14:25:29 +0000</pubDate>
		<dc:creator>shabda</dc:creator>
				<category><![CDATA[about]]></category>
		<category><![CDATA[apps]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://uswaretech.com/blog/?p=902</guid>
		<description><![CDATA[Summary: How to integrate a non Django database system in your Django code, using WordPress as example. The completed code is available at github or you can see some screnshots Though there are quite a few good Django blog applications, our blog is based on WordPress. A number of plugin&#8217;s make moving to a Django [...]


Related posts:<ol><li><a href='http://uswaretech.com/blog/2010/01/doing-things-with-django-forms/' rel='bookmark' title='Doing things with Django forms'>Doing things with Django forms</a></li>
<li><a href='http://uswaretech.com/blog/2010/01/django-models-tutorial/' rel='bookmark' title='Doing things with Django models &#8211; aka &#8211; Django models tutorial'>Doing things with Django models &#8211; aka &#8211; Django models tutorial</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p>Summary:
How to integrate a non Django database system in your Django code, using WordPress
as example. <a href="http://github.com/uswaretech/django-wordpress">The completed code is available at github</a> or you can <a href="#screenshots_wp">see some screnshots</a></p>

<hr />

<p>Though there are quite a <a href="http://github.com/montylounge/django-mingus">few good</a> <a href="http://byteflow.su/">Django blog</a> applications, our blog is based on
<a href="http://wordpress.org/">WordPress</a>. A <a href="http://mitcho.com/code/yarpp/">number</a> <a href="http://diythemes.com/">of</a> <a href="http://www.backtype.com/">plugin&#8217;s</a>
make moving to a Django based app a bad decision
for us, and not in the spirit of &#8220;best tools for the job&#8221;.</p>

<p>We moved the other way, and decided to use <a href="http://github.com/uswaretech/django-wordpress">Django to admin the
Wordpress database</a>. The completed code is available on <a href="http://github.com/uswaretech/django-wordpress">Github</a></p>

<p>It is not too hard, with the the builtin Django commands. Django provides the
<a href="http://www.djangobook.com/en/1.0/chapter16/"><code>inspectdb</code></a> command which allows you to build your models.py from an existing
non Django database.</p>

<p>Here we will see the steps followed for WordPress, but it would be about the same for all
systems.</p>

<h5>Take a back up of wordpress</h5>

<pre><code>mysqldump -u wordpress_user -p --database wordpress_database &gt; data.sql
</code></pre>

<h5>Create a new project, and set its settings to use the WordPress database.</h5>

<pre><code>    DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
    DATABASE_NAME = ''             # Or path to database file if using sqlite3.
    DATABASE_USER = ''             # Not used with sqlite3.
    DATABASE_PASSWORD = ''         # Not used with sqlite3.
    DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
    DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
</code></pre>

<h5>Get the initial models.py</h5>

<pre><code>./manage.py inspectdb &gt; models.py
</code></pre>

<p>This will create all the database tables in a form Django can understand. Here is what this command creates for a
my WordPress installation with the YARPP plugin. <a href="http://gist.github.com/278962">http://gist.github.com/278962</a></p>

<h5>Create a new app and put this models.py there.</h5>

<p>With this done, you can treat the non Django part as a
standalone application. Since WordPress appends all its table with <code>wp_</code> prefix,
we name this applications <code>wp</code> to maintain table name compatibility with Django naming
conventions.</p>

<p>You will notice that all models have the <code>db_table</code> populated, so we can rename tables, without changes to the database.</p>

<h5>Differences between WordPress and Django style naming.</h5>

<p>At this point you will notice some differences in how Django names things (in a
best practice sort of way), and how WordPress does it.</p>

<p>a. Django table and model class name are (generally) singular. eg <code>class Post(models.Models)</code> leads to table <code>app_post</code>.
Wordpress tables are (most of them) named plural eg <code>wp_posts</code>.</p>

<p>b. Django attributes are generally named without the table name part. Eg</p>

<pre><code>class Comment(models.Model):
    author_name = models.TextField()
    content = models.TextField()
</code></pre>

<p>WordPress is explicit here and includes the table prefix with attributes.</p>

<pre><code>mysql&gt; desc wp_comments;
+----------------------+---------------------+------+-----+---------------------+----------------+
| Field                | Type                | Null | Key | Default             | Extra          |
+----------------------+---------------------+------+-----+---------------------+----------------+
| comment_ID           | bigint(20) unsigned | NO   | PRI | NULL                | auto_increment | 
| comment_post_ID      | bigint(20) unsigned | NO   | MUL | 0                   |                | 
| comment_author       | tinytext            | NO   |     | NULL                |                | 
| comment_author_email | varchar(100)        | NO   |     |                     |                | 

.....
</code></pre>

<p>I believe this is due to the way you would generally be using the code. In Django you would do
<code>comment.author</code> where being explicit doesn&#8217;t add any value, while in WordPress, you would use,
<code>select comment_author, post_title ... from wp_comment, wp_post ... where join</code>, where being explicit
is useful.</p>

<p>You can decouple the Django and database names by using the <code>db_table</code> and <code>db_column</code> attributes.
We choose to rename the Class names to match Django conventions while we let the column names remain the same.</p>

<h5>Add Admin and other Django niceties.</h5>

<p>WordPress doesn&#8217;t (seem to) have foreign key constraints setup correctly, and
uses  <code>bigint(20) unsigned</code> without foreign key constraints to refer to referred entities.
This means Django creates all ForeignKeys as IntegerFields.</p>

<p>Modify them to use ForeignKey instead. Also add <code>__unicode__</code>, to your classes.</p>

<p>Add an <code>admin.py</code> to register all your classes.</p>

<p>And you are done! Now you can access, and work with your WordPress data inside Django
and Django admin.</p>

<hr />

<p>There are a few more things which will allow a easier WordPress setup.</p>

<h5>Create template tags to show the latest posts and comments.</h5>

<pre><code>@register.inclusion_tag("wp/recent_posts.html")
def show_posts(num_comments):
    return {"posts": Post.objects.filter(post_type="post", post_status="publish").order_by("-post_date")[:num_comments]}
</code></pre>

<p>So you can see that there is nothing WordPress specific we need too do here.</p>

<h5>Create a better admin.</h5>

<pre><code>Add ModelAdmin to generally used models.
</code></pre>

<h5>Allows accessing attributes via the Django style names.</h5>

<p>If you override <code>__getattr__</code>, you can access
the attributes via other names. Eg in the current setup you need to do <code>comment.comment_content</code>, <code>comment.comment_author</code> etc,
while we would like to do <code>comment.content</code>  and <code>comment.author</code> as a shortcut.</p>

<pre><code>class WordPressModel(object):    
    def __getattr__(self, v):
        if v in self.__dict__:
            return self.__dict__[v]
        else:
            new_v = "%s_%s" % (self.__class__.__name__.lower(),  v)
            if new_v in self.__dict__:
                return self.__dict__[new_v]
            else:
                raise AttributeError
</code></pre>

<p>It is highly debatable whether this is a good idea <img src='http://uswaretech.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , but it is too convenient right now not to test this method out.</p>

<p><a name="screenshots_wp" />
Here are some screenshots.</p>

<p><img alt="" src="http://uswaretech.com/dump/screenshots/screenshot_010.png" title="Wordpress django admin" class="alignnone" width="600" /></p>

<p><img alt="" src="http://uswaretech.com/dump/screenshots/screenshot_011.png" title="Wordpress django admin" class="alignnone" width="600" /></p>

<hr />

<p><a href="http://feeds.feedburner.com/uswarearticles">Do you subscribe to our feed</a>? We recently made a full text feed available, so if you are using the old feed, you should change it. <a href="http://feeds.feedburner.com/uswarearticles">Subscribe now</a>.</p>


<p>Related posts:<ol><li><a href='http://uswaretech.com/blog/2010/01/doing-things-with-django-forms/' rel='bookmark' title='Doing things with Django forms'>Doing things with Django forms</a></li>
<li><a href='http://uswaretech.com/blog/2010/01/django-models-tutorial/' rel='bookmark' title='Doing things with Django models &#8211; aka &#8211; Django models tutorial'>Doing things with Django models &#8211; aka &#8211; Django models tutorial</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/uswarearticles?a=oIyNNJU6mL0:vk3XYduH4Jg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=oIyNNJU6mL0:vk3XYduH4Jg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=oIyNNJU6mL0:vk3XYduH4Jg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=oIyNNJU6mL0:vk3XYduH4Jg:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=oIyNNJU6mL0:vk3XYduH4Jg:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=oIyNNJU6mL0:vk3XYduH4Jg:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/uswarearticles/~4/oIyNNJU6mL0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://uswaretech.com/blog/2010/01/wordpress-and-django-best-buddies/feed/</wfw:commentRss>
		<slash:comments>41</slash:comments>
		<feedburner:origLink>http://uswaretech.com/blog/2010/01/wordpress-and-django-best-buddies/</feedburner:origLink></item>
		<item>
		<title>Doing things with Django forms</title>
		<link>http://feedproxy.google.com/~r/uswarearticles/~3/IIX-DgkD1UQ/</link>
		<comments>http://uswaretech.com/blog/2010/01/doing-things-with-django-forms/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 11:26:18 +0000</pubDate>
		<dc:creator>shabda</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://uswaretech.com/blog/?p=881</guid>
		<description><![CDATA[Forms are one of the best features of Django. (After models, admin, url routing etc ). Here is a quick tutorial describing how to do things with Django forms. Basic form Prob. You want to show a form, validate it and display it. Ans. Create a simple form. class UserForm(forms.Form): username = forms.CharField() joined_on = [...]


Related posts:<ol><li><a href='http://uswaretech.com/blog/2008/10/dynamic-forms-with-django/' rel='bookmark' title='Dynamic forms with Django'>Dynamic forms with Django</a></li>
<li><a href='http://uswaretech.com/blog/2008/04/five-things-i-hate-about-django/' rel='bookmark' title='Five Things I Hate About Django.'>Five Things I Hate About Django.</a></li>
<li><a href='http://uswaretech.com/blog/2009/04/develop-twitter-api-application-in-django-and-deploy-on-google-app-engine/' rel='bookmark' title='Develop Twitter API application in django and deploy on Google App Engine'>Develop Twitter API application in django and deploy on Google App Engine</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p>Forms are one of the best features of Django. (After models, admin, url routing etc <img src='http://uswaretech.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ). Here is a quick tutorial describing how to do things with Django forms.</p>

<ol>
<li>Basic form</li>
</ol>

<p>Prob. You want to show a form, validate it and display it.</p>

<p>Ans. Create a simple form.</p>

<pre><code>class UserForm(forms.Form):
    username = forms.CharField()
    joined_on = forms.DateField()
</code></pre>

<p>This wil take care that the form is displayed with two text field,
and a value for them are filled in, and the second field has correct formatting for a date.</p>

<p>2.</p>

<p>Prob. Create a form which has values populated depending upon a runtime value,
eg you might want to show only the values corresponding to the current subdomain.</p>

<p>Ans. Create a form with an custom <code>__init__</code>.</p>

<pre><code>class UserForm(forms.Form):
    username = forms.CharField()
    plan = forms.ModelChoiceField(queryset = Plan.objects.none())

    def __init__(self, subdomain, *args, **kwargs):
        self.default_username = default_username
        super(UserForm, self).__init__(*args, **kwargs)
        self.fields['plan'].queryset = Plan.objects.filter(subdomain = subdomain)
</code></pre>

<p>Here in the <code>__init__</code> we are overriding the default queryset on field <code>plan</code>. Any of the attributes can similarly be overridden.</p>

<p>However the <code>self.fields</code> is populated only after <code>super(UserForm, self).__init__(*args, **kwargs)</code> is called.</p>

<p>3.</p>

<p>Prob. The same form is used in multiple views, and handles the <code>cleaned_data</code> similarly.</p>

<p>Ans. Create a form with a custom .save()</p>

<pre><code>class UserForm(forms.Form):
    username = forms.CharField()

    def save(self):
        data = self.cleaned_data
        user = User.objects.create(username = data['username'])
        #create a profile
        UserProfile.objects.create(user = user, ...some more data...)
</code></pre>

<p>You could have called this method anything, but this is generally called save, to maintain similarity with <code>ModelForm</code></p>

<p>4.</p>

<p>Prob. You need to create a form with fields which have custom validations.</p>

<p>Ans. Create a form with custom clean_fieldname</p>

<pre><code>class UserForm(forms.Form):
    username = forms.CharField()

    def clean_username(self):
        data = self.cleaned_data
        try:
            User.objects.get(username = data['username'])
        except User.DoesNotExist:
            return data['username']
        raise forms.ValidationError('This username is already taken.')
</code></pre>

<p>Here we can validate that the usernames are not repeated.</p>

<p>5.</p>

<p>Prob. You want to create a field which has cross field validations.</p>

<p>Ans. Create a field with a .clean</p>

<pre><code>class UserForm(forms.Form):
    username = forms.CharField()

    password1 = forms.PasswordField()
    password2 = forms.PasswordField()

    def clean(self):
        data = self.cleaned_data
        if "password1" in data and "password2" in data and data["password1"] != data["password2"]:
            raise forms.ValudationError("Passwords must be same")
</code></pre>

<p>6.</p>

<p>Problem.</p>

<p>You need a form the fields of which depends on some value in the database.
Eg. The field to be shown are customisable per user.</p>

<p>Create a form dynamically</p>

<pre><code>def get_user_form_for_user(user):
        class UserForm(forms.Form):
            username = forms.CharField()
            fields = user.get_profile().all_field()
            #Use field to find what to show.
</code></pre>

<p><a href="http://uswaretech.com/blog/2008/10/dynamic-forms-with-django/">This post provides much more details</a></p>

<p>7.</p>

<p>Prob. You need to create a Html form which writes to multiple Django models.</p>

<p>Ans. Django forms are not a one to one mapping to Html forms.</p>

<pre><code>#in forms.py
class UserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ["username", "email"]

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile

#in views.py
def add_user(request):
    ...
    if request.method == "POST":
        uform = UserForm(data = request.POST)
        pform = UserProfileForm(data = request.POST)
        if uform.is_valid() and pform.is_valid():
            user = uform.save()
            profile = pform.save(commit = False)
            profile.user = user
            profile.save()
            ....
    ...

#in template
&lt;form method="post"&gt;
    {{ uform.as_p }}
    {{ pform.as_p }}
    &lt;input type="submit" ...&gt;
&lt;/form&gt;
</code></pre>

<p>8.</p>

<p>Prob. You want to use multiple forms of same type on one page.</p>

<p>Ans.</p>

<p>a. If you want a datagrid style ui, use formset.</p>

<pre><code>from django.forms.formsets import formset_factory
forms = formset_factory(UserForm, extra = 4)
#
</code></pre>

<p><a href="http://docs.djangoproject.com/en/dev/topics/forms/formsets/">Formsets are described much more comprehensively here</a></p>

<p>b. If you do not need a datagrid style ui, use <code>prefix</code> argument to forms.</p>

<pre><code>Eg. you have a survey app, and you want a page with all questions from that survey displayed.

#IN views.py
def survey(request, survey_slug)
    ...
    questions = survey.questions.all()
    question_forms = []
    for question in questions:
        qform = QuestionForm(question=question, prefix = question.slug)
        question_forms.append(qform)
        ...
    if request.method == "POST":
        for question in questions:
            qform = QuestionForm(question=question, prefix = question.slug, data = request.POST)
        #Validate and do save action
        ...
    ...
</code></pre>

<hr />

<p><a href="http://feeds.feedburner.com/uswarearticles">Do you subscribe to our feed</a>? We recently made a full text feed available, so if you are using the old feed, you should change it. <a href="http://feeds.feedburner.com/uswarearticles">Subscribe now</a>.</p>


<p>Related posts:<ol><li><a href='http://uswaretech.com/blog/2008/10/dynamic-forms-with-django/' rel='bookmark' title='Dynamic forms with Django'>Dynamic forms with Django</a></li>
<li><a href='http://uswaretech.com/blog/2008/04/five-things-i-hate-about-django/' rel='bookmark' title='Five Things I Hate About Django.'>Five Things I Hate About Django.</a></li>
<li><a href='http://uswaretech.com/blog/2009/04/develop-twitter-api-application-in-django-and-deploy-on-google-app-engine/' rel='bookmark' title='Develop Twitter API application in django and deploy on Google App Engine'>Develop Twitter API application in django and deploy on Google App Engine</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/uswarearticles?a=IIX-DgkD1UQ:FTv4lRAJRLc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=IIX-DgkD1UQ:FTv4lRAJRLc:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=IIX-DgkD1UQ:FTv4lRAJRLc:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=IIX-DgkD1UQ:FTv4lRAJRLc:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=IIX-DgkD1UQ:FTv4lRAJRLc:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=IIX-DgkD1UQ:FTv4lRAJRLc:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/uswarearticles/~4/IIX-DgkD1UQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://uswaretech.com/blog/2010/01/doing-things-with-django-forms/feed/</wfw:commentRss>
		<slash:comments>42</slash:comments>
		<feedburner:origLink>http://uswaretech.com/blog/2010/01/doing-things-with-django-forms/</feedburner:origLink></item>
		<item>
		<title>Tools of Pro Django developer – aka What powers dinette and almost every app we write.</title>
		<link>http://feedproxy.google.com/~r/uswarearticles/~3/rjdULjrbPmo/</link>
		<comments>http://uswaretech.com/blog/2010/01/tools-of-pro-django-developer/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 12:32:11 +0000</pubDate>
		<dc:creator>shabda</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[django]]></category>

		<guid isPermaLink="false">http://uswaretech.com/blog/?p=870</guid>
		<description><![CDATA[There are some tools and apps which we use with almost all apps we write, and in particular which, we used for dinette. Here they are broken into useful during development, and (also) useful post development. During Development Ipython and ipdb South Django test utils Django extensions Django debug toolbar Ipython and ipdb Ipython is [...]


Related posts:<ol><li><a href='http://uswaretech.com/blog/2009/11/django-for-a-rails-developer/' rel='bookmark' title='Django for a Rails Developer'>Django for a Rails Developer</a></li>
<li><a href='http://uswaretech.com/blog/2010/01/django-models-tutorial/' rel='bookmark' title='Doing things with Django models &#8211; aka &#8211; Django models tutorial'>Doing things with Django models &#8211; aka &#8211; Django models tutorial</a></li>
<li><a href='http://uswaretech.com/blog/2009/02/how-to-build-a-facebook-app-in-django/' rel='bookmark' title='How to build a Facebook app in Django'>How to build a Facebook app in Django</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p>There are some tools and apps which we use with almost all apps we write, and in particular which,
we used for <a href="http://uswaretech.com/forum/">dinette</a>. Here they are broken into useful during development,
and (also) useful post development.</p>

<h4>During Development</h4>

<ol>
<li><a href="http://ipython.scipy.org/">Ipython</a> and <a href="http://pypi.python.org/pypi/ipdb">ipdb</a></li>
<li><a href="http://south.aeracode.org/">South</a></li>
<li><a href="http://ericholscher.com/projects/django-test-utils/">Django test utils</a></li>
<li><a href="http://github.com/django-extensions/django-extensions">Django extensions</a></li>
<li><a href="http://github.com/robhudson/django-debug-toolbar">Django debug toolbar</a></li>
</ol>

<h5><a href="http://ipython.scipy.org/">Ipython</a> and <a href="http://pypi.python.org/pypi/ipdb">ipdb</a></h5>

<p>Ipython is a enhanced shell for python. Ipdb similarly add extra capacity to the builtin pdb debugger.
It is extremely convenient to drop into a ipython shell right where a complex piece of code is being hit.</p>

<pre><code>from IPython.Shell import IPShellEmbed
ipython = IPShellEmbed()
ipython()
</code></pre>

<p>Of course this does not allow you to step through, so if you want to step through your code, you need to do</p>

<pre><code>import ipdb
ipdb.set_trace()
</code></pre>

<p>Of course, you can just do <code>pdb.set_trace()</code>, but with tab completion and syntax highlighting, using ipdb is a no brainer.</p>

<h5><a href="http://south.aeracode.org/">South</a></h5>

<p>South adds schema migration to Django. It has lot of advanced options, but just three command will get you along way.</p>

<p>Convert a normal app to use the south way.</p>

<pre><code>./manage.py convert_to_south
</code></pre>

<p>Once you have made any changes to your apps model, write a new migration which can update the database to latest models.py</p>

<pre><code>./manage.py startmigration appname nameofmigration --auto
</code></pre>

<p>Once you have written a migration, write a command to update the db.</p>

<pre><code>./manage.py migrate appanme 
</code></pre>

<h5><a href="http://ericholscher.com/projects/django-test-utils/">Django test utils</a></h5>

<p>When you are running tests, a huge time sink is the time spent dropping and recreating tests. You might add just a single test,
and the default Django testrunner will spend a huge time recreating the database. Enter <code>manage.py quicktest</code> which reuses databases
between test runs. One of the side effects of this is that if you modify <code>models.py</code> between test runs you should manually
recreate the database.</p>

<p>The tests (or whatever little of it exists) for Dinette were written using testutils&#8217;
<a href="http://github.com/ericholscher/django-test-utils/tree/master/test_utils/testmaker/">testmaker</a>.</p>

<h5><a href="http://github.com/django-extensions/django-extensions">Django extensions</a></h5>

<p>Django extensions, (earlier Django command extensions) is a app which adds extra commands to <code>manage.py</code>. It has lot of goodies, but just
<code>./manage.py shell_plus</code> makes it worthwhile. (It imports all the models from all the apps automatically).</p>

<p>Get the full list at <a href="http://wiki.github.com/django-extensions/django-extensions/current-command-extensions">Github</a></p>

<h5><a href="http://github.com/robhudson/django-debug-toolbar">Django debug toolbar</a></h5>

<p>I do not personally use it but some people on our <a href="http://uswaretech.com/team/">team</a> swear by it.</p>

<h3>After development</h3>

<ol>
<li><a href="http://github.com/ericflo/django-pagination">Django-pagination</a></li>
<li><a href="http://thumbnail.sorl.net/docs/">sorl-thumbnails</a></li>
<li><a href="http://github.com/mintchaos/django_compressor/">django-compressor</a></li>
<li><a href="http://haystacksearch.org">Haystack</a></li>
</ol>

<h5><a href="http://github.com/ericflo/django-pagination">Django-pagination</a></h5>

<p>I have decided on a standard way to build apps which need pagination. a. Build apps without pagination. b. The night
before release spend an hour and add pagination to all pages. Really this is all you need</p>

<pre><code>{% load pagination tags %}
{% autopaginate queryset %}
....
{% paginate %}
</code></pre>

<p>There is no change required to the views.</p>

<h5><a href="http://thumbnail.sorl.net/docs/">sorl-thumbnails</a></h5>

<p>To thumbnail an image.</p>

<pre><code>{% thumbnail image size %}
</code></pre>

<p>&#8220;Things should be as simple as possible, but not simpler.&#8221;</p>

<h5><a href="http://github.com/mintchaos/django_compressor/">django-compressor</a></h5>

<p>During development we work with multiple unminified js and css files. Django-compressor takes care of minifying and compressing
it when we deploy. All we need to do is,</p>

<pre><code>{% compress css %}
...
all css files
....
{% endcompress %}


{% compress js %}
...
all js files
....
{% endcompress %}
</code></pre>

<h5><a href="http://haystacksearch.org">Haystack</a></h5>

<p>Haystack is &#8220;modular search for Django&#8221;. It make writing search views as easy as writing the admin. You declaratively tell what
fields you want to search on, and Haystack can take care of creating and updating the index, and providing a generic view
to handle the search. Check a search view written using Haystack <a href="http://uswaretech.com/forum/search/?q=django">http://uswaretech.com/forum/search/?q=django</a></p>

<hr />

<p>To everyone who contributed to these apps, thanks. Django development won&#8217;t be the same without these great tools.
This post was inspired by <a href="http://montylounge.com/">Kevin Fricovsky&#8217;s post</a> post, <a href="http://blog.montylounge.com/2009/sep/24/apps-that-power-django-mingus/">The apps that power Django-Mingus</a>
and is stolen about 50% from there. Thanks. <img src='http://uswaretech.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<hr />

<p>We build Amazing web apps, and we can build it for you. <a href="http://uswaretech.com/contact/">Contact us today</a>.</p>


<p>Related posts:<ol><li><a href='http://uswaretech.com/blog/2009/11/django-for-a-rails-developer/' rel='bookmark' title='Django for a Rails Developer'>Django for a Rails Developer</a></li>
<li><a href='http://uswaretech.com/blog/2010/01/django-models-tutorial/' rel='bookmark' title='Doing things with Django models &#8211; aka &#8211; Django models tutorial'>Doing things with Django models &#8211; aka &#8211; Django models tutorial</a></li>
<li><a href='http://uswaretech.com/blog/2009/02/how-to-build-a-facebook-app-in-django/' rel='bookmark' title='How to build a Facebook app in Django'>How to build a Facebook app in Django</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/uswarearticles?a=rjdULjrbPmo:2RfN4zRj1Oo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=rjdULjrbPmo:2RfN4zRj1Oo:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=rjdULjrbPmo:2RfN4zRj1Oo:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=rjdULjrbPmo:2RfN4zRj1Oo:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=rjdULjrbPmo:2RfN4zRj1Oo:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=rjdULjrbPmo:2RfN4zRj1Oo:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/uswarearticles/~4/rjdULjrbPmo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://uswaretech.com/blog/2010/01/tools-of-pro-django-developer/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		<feedburner:origLink>http://uswaretech.com/blog/2010/01/tools-of-pro-django-developer/</feedburner:origLink></item>
		<item>
		<title>django-forum</title>
		<link>http://feedproxy.google.com/~r/uswarearticles/~3/DtRP4S1d6YA/</link>
		<comments>http://uswaretech.com/blog/2010/01/django-forum/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 11:19:03 +0000</pubDate>
		<dc:creator>shabda</dc:creator>
				<category><![CDATA[apps]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[utilities]]></category>

		<guid isPermaLink="false">http://uswaretech.com/blog/?p=856</guid>
		<description><![CDATA[twitter ready version: We have released a Django forum application, with some cool features not in any other Django based forum. You can get it here or see it in action. blog version There are quite a few Django based forum applications, so why another? Its a bit of a rhetorical question, as the answer [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p></p><h3>twitter ready version:</h3>

<p>We have released a <a href="http://uswaretech.com/forum/">Django forum</a> application, with some cool features not in any other Django based forum. <a href="http://github.com/uswaretech/Dinette">You can get it here</a> or <a href="http://uswaretech.com/forum/">see it in action</a>.</p>

<h3>blog version</h3>

<p>There are quite a few <a href="http://code.djangoproject.com/wiki/ForumAppsComparison">Django based forum applications</a>, so why another? Its a bit of a rhetorical question,  as the answer is &#8220;none of them met my needs exactly, so we created my own&#8221;, as you might expect.</p>

<p>Without further ado here is a list of features. It is available on <a href="http://uswaretech.com/forum/">uswaretech.com/forum/</a>.</p>

<ul>
<li>Based on, inspired by and ripped from <a href="http://punbb.informer.com/">PunBB</a>. (Thanks!)</li>
<li>PunBB like 3 phased hierarchy [Forum]->Subforum->Topic->Post</li>
<li>Tightly integrated with <a href="http://github.com/uswaretech/Django-Socialauth">Socialauth</a>. (Screenshots)</li>
<li><a href="http://www.gravatar.com/">Gravatar</a> support</li>
<li>Moderation features (Close, stikify, make announcement etc.)</li>
<li>Ajax based posting</li>
</ul>

<p>We are starting our forums based on this app, so we plan to be supporting and developing this for foreseeable future. <a href="http://github.com/uswaretech/Dinette">Fork this on Github</a> or <a href="http://uswaretech.com/forum/">check this out now</a>.</p>

<p><a name="screenshots"></a></p>

<h3>Screenshots</h3>

<p>Main page</p>

<p><a href="http://uswaretech.com/dump/screenshots/screenshot_007.png"><img alt="" src="http://uswaretech.com/dump/screenshots/screenshot_007.png" title="Dinette screenshot" class="alignnone" width="640" height="420" /></a> <br /> <br /></p>

<p>Post page</p>

<p><a href="http://uswaretech.com/dump/screenshots/screenshot_008.png"><img alt="" src="http://uswaretech.com/dump/screenshots/screenshot_008.png" title="Dinette screenshot" class="alignnone"  width="640" height="420" /></a> <br /> <br /></p>

<p>Login Page</p>

<p><a href="http://uswaretech.com/dump/screenshots/screenshot_009.png"><img alt="" src="http://uswaretech.com/dump/screenshots/screenshot_009.png" title="Dinette screenshot" class="alignnone"  width="640" height="420" /></a> <br /> <br /></p>

<hr />

<p>We build amazing web apps. <a href="http://uswaretech.com/contact/">Contact us</a></p>


<p>No related posts.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/uswarearticles?a=DtRP4S1d6YA:iLy67zhot9Q:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=DtRP4S1d6YA:iLy67zhot9Q:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=DtRP4S1d6YA:iLy67zhot9Q:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=DtRP4S1d6YA:iLy67zhot9Q:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/uswarearticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/uswarearticles?a=DtRP4S1d6YA:iLy67zhot9Q:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/uswarearticles?i=DtRP4S1d6YA:iLy67zhot9Q:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/uswarearticles/~4/DtRP4S1d6YA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://uswaretech.com/blog/2010/01/django-forum/feed/</wfw:commentRss>
		<slash:comments>62</slash:comments>
		<feedburner:origLink>http://uswaretech.com/blog/2010/01/django-forum/</feedburner:origLink></item>
	</channel>
</rss>

