<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>From Accessibility to Zope &#187; Django</title>
	<atom:link href="http://blog.mauveweb.co.uk/category/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mauveweb.co.uk</link>
	<description>experiments in contemporary web development</description>
	<lastBuildDate>Fri, 30 Jul 2010 13:09:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Model Downcast in Django</title>
		<link>http://blog.mauveweb.co.uk/2008/07/30/model-downcast-in-django/</link>
		<comments>http://blog.mauveweb.co.uk/2008/07/30/model-downcast-in-django/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 20:29:43 +0000</pubDate>
		<dc:creator>mauve</dc:creator>
				<category><![CDATA[Django]]></category>

		<guid isPermaLink="false">http://blog.mauveweb.co.uk/?p=154</guid>
		<description><![CDATA[Django has had multi-table inheritance for three months now, but I haven't used it because I can't picture many use cases for inheritance which don't rely on dynamic polymorphism.
In my current project, however, I've found a fairly convincing use case. I can do all of the basic listing and manipulation with the base class, and [...]]]></description>
			<content:encoded><![CDATA[<p>Django has had multi-table inheritance for three months now, but I haven't used it because I can't picture many use cases for inheritance which don't rely on dynamic polymorphism.</p>
<p>In my current project, however, I've found a fairly convincing use case. I can do all of the basic listing and manipulation with the base class, and I only need the superclass for one particular operation. </p>
<p>Rather than writing this in a different way, which would be less intuitive, I pushed ahead with the conceptually simple approach and fought with Django to make the inheritance polymorphic. The way I did this was brute force it: check all of the relations for one that exists.</p>
<p><code><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">db</span> <span style="color: #ff7700;font-weight:bold;">import</span> models<br />
cls = inst.__class__ <span style="color: #808080; font-style: italic;">#inst is an instance of the base model</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> r <span style="color: #ff7700;font-weight:bold;">in</span> cls._meta.<span style="color: black;">get_all_related_objects</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">issubclass</span><span style="color: black;">&#40;</span>r.<span style="color: black;">model</span>, cls<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> \<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>r.<span style="color: black;">field</span>, models.<span style="color: black;">OneToOneField</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">continue</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>inst, r.<span style="color: black;">get_accessor_name</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span> models.<span style="color: black;">ObjectDoesNotExist</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">continue</span></code></p>
<p>There are faster ways of doing this if you do it at the database level (left joining all tables in the inheritance hierarchy and sorting out the mess into the correct subclasses), but this is simple and self-contained and forces me to think about when I need this. And looking at the query cache, the overhead is not too bad anyway.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.mauveweb.co.uk/2008/07/30/model-downcast-in-django/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Satchmo</title>
		<link>http://blog.mauveweb.co.uk/2008/01/07/satchmo/</link>
		<comments>http://blog.mauveweb.co.uk/2008/01/07/satchmo/#comments</comments>
		<pubDate>Mon, 07 Jan 2008 12:21:49 +0000</pubDate>
		<dc:creator>mauve</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[e-Commerce]]></category>

		<guid isPermaLink="false">http://blog.mauveweb.co.uk/2008/01/07/satchmo/</guid>
		<description><![CDATA[Since I've been using the Django framework for web development I've been pointed in the direction of the Satchmo framework a few times. Satchmo is intended to be the framework for e-Commerce development built on top of Django. I've looked at Satchmo more than once trying to weigh up whether it would be a useful [...]]]></description>
			<content:encoded><![CDATA[<p>Since I've been using the Django framework for web development I've been pointed in the direction of the <a href="http://www.satchmoproject.com/">Satchmo</a> framework a few times. Satchmo is intended to be <em>the</em> framework for e-Commerce development built on top of Django. I've looked at Satchmo more than once trying to weigh up whether it would be a useful thing to use for projects, because an e-Commerce framework certainly should be useful. Satchmo doesn't satisfy this expectation.</p>
<p>I have written an e-Commerce shop in Django. It took three days, much of which was spent writing the backend &#8211; basket, order processing and so on. I am absolutely certain that I couldn't have done that with Satchmo. The first reason is that Satchmo isn't orthogonal; it's impossible to use Satchmo's basket/ordering system without using Satchmo's products front end. Front ends are very easy to write in Django and I needed something custom for this shop. Custom front ends are very hard to write in Satchmo &#8211; they involve writing custom extensions to the product model. The second reason is that Satchmo doesn't use Enterprise Design Patterns like <a href="http://martinfowler.com/eaaCatalog/money.html">Money</a>.</p>
<p>Satchmo also provides code that is very much outside its remit as a framework, like <a href="http://www.satchmoproject.com/trac/wiki/SatchmoAnalytics">this strange configuration property which embeds Google Analytics</a>. Google Analytics code is trivial to paste into the templates should I want to do so. Why would I need this built into the platform?</p>
<p>Satchmo doesn't satisfy as a framework; it's more of an off-the-shelf application regardless of what the authors claim. Now I have Django web shop code, I won't be needing Satchmo.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.mauveweb.co.uk/2008/01/07/satchmo/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>More on Django</title>
		<link>http://blog.mauveweb.co.uk/2007/07/02/more-on-django/</link>
		<comments>http://blog.mauveweb.co.uk/2007/07/02/more-on-django/#comments</comments>
		<pubDate>Mon, 02 Jul 2007 22:28:54 +0000</pubDate>
		<dc:creator>mauve</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://blog.mauveweb.co.uk/2007/07/02/more-on-django/</guid>
		<description><![CDATA[As I come to the end of my first project using Django, I can offer a slightly better picture of how Django actually measures up in the field. In general I found that the bulk of the application was easier than I'd expect, but the remainder was more time consuming than I'd expected.
As mentioned before, [...]]]></description>
			<content:encoded><![CDATA[<p>As I come to the end of my first project using Django, I can offer a slightly better picture of how Django actually measures up in the field. In general I found that the bulk of the application was easier than I'd expect, but the remainder was more time consuming than I'd expected.<span id="more-78"></span></p>
<p>As mentioned before, Django's weakest link is its rudimentary template language. Content and data has to be digested and spoon-fed to the templates because the templating language doesn't provide the tools required to manipulate data arbitrarily. This doesn't allow the separation of presentation and controller code that is the purpose of a templating language. Instead, each template has a one-to-one dependence on the code which sets up the model. On the plus side, it's very easy to track where the thread of execution goes in these templates. A potentially dangerous issue is that the templating system defaults to not escaping data variables. This is effectively <acronym title="Cross-site scripting">XSS</acronym> unaware. Inverting the semantics such as to escape everything unless I tell you otherwise (to insert <acronym title="eXtensible Markup Language">XML</acronym>/SGML markup, say) would fix this.</p>
<p>The philosophy given by the Django documentation for the design of its templating is that</p>
<blockquote><p>We wanted Django’s template language to be usable for more than just <acronym title="eXtensible Markup Language">XML</acronym>/HTML templates. At World Online, we use it for e-mails, JavaScript and <acronym title="Comma Separated Value">CSV</acronym>. You can use the template language for any text-based format.</p></blockquote>
<p>This "one size fits all" approach is self-evidently misguided. <acronym title="eXtensible Markup Language">XML</acronym> and JSON need a guarantee of structural validity but they rarely need re-styling anyway. (X)<acronym title="HyperText Markup Language">HTML</acronym> needs escaping. Javascript (other than JSON) should be static anyway. <acronym title="Comma Separated Value">CSV</acronym> barely needs an <acronym title="	Application Programming Interface">API</acronym>, let along a templating language. Emails&#8230; well, OK, this is a fine templating language for text-only emails.</p>
<p>I'd also found my Django application hard to structure. Views (functions called by the Django context in response to different request URIs) shouldn't contain application code. Following that philosophy, my code is split mainly into four classes in two Django "applications", a handful of utility functions in the <acronym title="Object-Relational Mapper">ORM</acronym> models, and everything else is wrapped up in the 20 or so views. They don't do more than delegation and template context setup, but there be semantics in them there views, and those semantics aren't codified a form that can be nested and built into more advanced and interdependent web applications. For larger applications, Django will need code to modify and extend the framework, but there isn't a good <acronym title="	Application Programming Interface">API</acronym> to plug in this code. It's a mixture of adding the class names to the config file, and giving modules special names within the package.</p>
<p>These issues tends to convey to me the general sense that Django code is hard to tie together. Overall, to build an application as elegantly structured as my current web shop code would require a sizable layer on top of the Django framework, with the occasional aspect of Django re-written too. But then, my current shop code has already borrowed some of the best aspects of programming with Django as well as a handful of other frameworks too <img src='http://blog.mauveweb.co.uk/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.mauveweb.co.uk/2007/07/02/more-on-django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django</title>
		<link>http://blog.mauveweb.co.uk/2007/04/06/django/</link>
		<comments>http://blog.mauveweb.co.uk/2007/04/06/django/#comments</comments>
		<pubDate>Fri, 06 Apr 2007 13:06:21 +0000</pubDate>
		<dc:creator>mauve</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://blog.mauveweb.co.uk/2007/04/06/django/</guid>
		<description><![CDATA[For the past couple of weeks I've been developing a web application for a client in the Django framework. I looked at frameworks before I started, wondering what I could use to make the development of this project easier.
I have previously mentioned why I abhor PHP even as I developed Mauvespace in that language, but [...]]]></description>
			<content:encoded><![CDATA[<p>For the past couple of weeks I've been developing a web application for a client in the Django framework. I looked at frameworks before I started, wondering what I could use to make the development of this project easier.</p>
<p>I have previously mentioned why I abhor <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> even as I developed Mauvespace in that language, but as I started this project <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> seemed utterly unconscionable. <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> doesn't offer anything apart from a dodgy syntax, incomplete object model and an <acronym title="	Application Programming Interface">API</acronym> consisting of a basic <acronym title="Common Gateway Interface">CGI</acronym> wrapper and bindings for a few library functions. Alternatives included my Python framework, but that is a long way from being ready for the mainstream. In fact it's been all but abandoned. I originally wrote it to serve static sites generated from <acronym title="eXtensible Stylesheet Language">XSL</acronym> and <acronym title="eXtensible Markup Language">XML</acronym>, but then found myself bolting in little bits of <acronym title="Common Gateway Interface">CGI</acronym> scripts and eventually writing a whole <acronym title="Object-Relational Mapper">ORM</acronym>.</p>
<p>I had also been looking at Zope, and particularly Zope 3, but Zope has a ridiculous learning curve. It does seem appropriate for more large-scale, extensible development though. It's just that most web application projects for SMEs don't need to be so enterprise that they require unit tests for everything and interfaces saying what each class will do as well as an implementation. I am looking into Zope but I don't expect to find a project that it will be suitable for very often.</p>
<p>So Django then. I'd seen Django before and discounted it because of its templating language, which was not what I wanted. I wanted to use <acronym title="eXtensible Stylesheet Language">XSL</acronym>, obviously. <acronym title="eXtensible Stylesheet Language">XSL</acronym> and <acronym title="eXtensible Markup Language">XML</acronym> can together be handed to browsers. This makes writing <acronym title="Asynchronous JavaScript and XML">AJAX</acronym> applications much easier: no special handlers are required to be able to pass <acronym title="eXtensible Markup Language">XML</acronym> data to the client. With the same requests, Javascript can create whatever <acronym title="REpresenational State Transfer">REST</acronym> model it likes.</p>
<p>But regardless, it looked more appropriate than anything else at this point.</p>
<p>So Django then. I was really impressed with Django initially. I say initially because while I managed to write about half of the application in the first two hours of using it, the rest has drawn out and out.</p>
<p>Django has an excellent <acronym title="Object-Relational Mapper">ORM</acronym>. However, it makes the mistake of trying to encapsulate all of <acronym title="Structured Query Language">SQL</acronym> in the Python Object Model, which is a kind of antipattern in itself. This happens all the time with <acronym title="Object-Relational Mappers">ORMs</acronym>, because while it's easy to make database rows appear to be objects, <acronym title="Structured Query Language">SQL</acronym> is designed to do clever things that you don't really want to re-implement in another layer just because you can abstract it. Django's <acronym title="Object-Relational Mapper">ORM</acronym> allows users to write code like Game.objects.get(id=the_game) to retrieve a single game. That's lovely. Then it allows you to write code like Game.objects.filter(category=this_category), and it <em>lazily</em> evaluates the query, even allowing you to chain these filters lazily. Which is fantastic.</p>
<p>Then you hit queries like finding objects with date ranges. In <acronym title="Structured Query Language">SQL</acronym> this is easy.</p>
<p><code><span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> games <span style="color: #993333; font-weight: bold;">WHERE</span> start &lt;= NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AND</span> end &gt; NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></code></p>
<p>In Django this is munged into a horrific double-underscore variable name:</p>
<p><code>now=<span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
Game.<span style="color: black;">objects</span>.<span style="color: #008000;">filter</span><span style="color: black;">&#40;</span>start__lte=now, end__gt=now<span style="color: black;">&#41;</span></code></p>
<p>If you're a programmer this should be fearing you with dread as to what comes next. Two sets or more of double-underscores starts performing joins, I believe.</p>
<p>So Django's <acronym title="Object-Relational Mapper">ORM</acronym> is a bit unwieldy, but this should come as no surprise because I think many <acronym title="Object-Relational Mappers">ORMs</acronym> are like this. One that I have seen that avoid this is Joomla's, where you just hand off an <acronym title="Structured Query Language">SQL</acronym> query to the <acronym title="Object-Relational Mapper">ORM</acronym> and it just populates objects from the results. Trying to construct complex queries in an object-oriented way is likely to be harder and less maintainably than just writing the <acronym title="Structured Query Language">SQL</acronym> query, and so I think this is the best solution in general.</p>
<p>Django's <acronym title="Object-Relational Mapper">ORM</acronym> libraries handily integrate with other bits of the system, such as administration screens, meaning almost no work has to be done to make your database rows entirely editable, with configurable permissions, from the admin screen. This is a big win for Django. There's no way you could even contemplate this with, say, Zope, because the <acronym title="Zope Object DataBase">ZODB</acronym> stores arbitrary objects and an admin screen would have to deal with, basically, all of Python.</p>
<p>The annoyances I've found later on in the development of Django is that it's not intuitive. There's a vast wealth of features, and you have to keep looking at the documentation to find them all. There is a <strong>lot</strong> of magic involved. This is Python's fault, really. Magic is easy to do in Python, and a lot of developers won't have any issues with jumping in and creating magic for ever situation. This means that there is much less code to be written for performing many tasks, but magic has drawbacks. Developers have to understand what the magic is doing for them. In languages where magic is hard, developers can read the code and know, with very little context, exactly what it will do and why. With magic, the object model and even elements of the language can be warped. I'd agree that magic is good for an ORM: it should mean that you can get an object that magically persists. You don't need to know what's going on to make it persist. However, Django goes way too far with the magic. Admin screens are driven by magic variables. The <acronym title="Uniform Resource Locator">URL</acronym> configuration is slightly magical. I think permissions are almost entirely magical but I haven't got into that yet.</p>
<p>Then there's the templating language. I was set to hate this from the beginning. There is a much-vaunted feature called template inheritance (according to the documentation "the real power of Django templates". Apparently this allows one template to extend another. In practice this doesn't do anything more than allowing you to stuff some chunk of content into another template. The only situation this appears to be useful is in stuffing the main content of your page into a layout. Woo. It doesn't allow you to do really useful things, like calling templates that create generic page elements with arbitrary content. At least, without creating an inheritance tree of dozens of little template element files in a manner that is likely to be quite unmaintainable. Templates can read Python variables and attributes of Python objects that are passed to them. They can apply "filters" which are written in the core Python code. They can't do anything else, like even computing simple expressions. This is annoying; few other templating languages are so restricted.</p>
<p>In future it's likely that I'll write a Django middleware to do <acronym title="eXtensible Stylesheet Language">XSL</acronym> transformation. Then I won't need Django's stupid templating. This doesn't sound like it will be too hard.</p>
<p>I'm currently trying to solve a problem with User authentication. I have a function, tickets_remaining() which I wanted to add to the User authentication class. But I can't do this. I need to create a separate Profile model and retrieve this instead.</p>
<p>So Django: on the whole, I'm impressed with it. The design is easy, extensible, and fast to get going with, but there are some caveats with the <acronym title="	Application Programming Interface">API</acronym> and particularly the templating.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.mauveweb.co.uk/2007/04/06/django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
