412 lines
16 KiB
HTML
412 lines
16 KiB
HTML
<?xml version="1.0" encoding="utf-8" ?>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="generator" content="Docutils 0.6: http://docutils.sourceforge.net/" />
|
|
<title></title>
|
|
<style type="text/css">
|
|
|
|
h1, h2, h3, h4,
|
|
#table-of-contents
|
|
{
|
|
color: #47c;
|
|
}
|
|
h1 { padding-top: 15px; }
|
|
h2 { padding-top: 10px; }
|
|
h3 { padding-top: 7px; }
|
|
|
|
a:hover { border-bottom: 1px solid #0066cc; }
|
|
a {color: #0066cc; text-decoration: none;}
|
|
|
|
li {
|
|
padding-top: 5px;
|
|
padding-bottom: 5px;
|
|
}
|
|
|
|
tt {
|
|
color: #080;
|
|
}
|
|
|
|
blockquote tt {
|
|
color: #000
|
|
}
|
|
|
|
.first {
|
|
margin-top: 0 }
|
|
|
|
.last {
|
|
margin-bottom: 0 }
|
|
|
|
/*
|
|
a.toc-backref {
|
|
text-decoration: none ;
|
|
color: black }
|
|
*/
|
|
|
|
dd {
|
|
margin-bottom: 0.5em }
|
|
|
|
div.abstract {
|
|
margin: 2em 5em }
|
|
|
|
div.abstract p.topic-title {
|
|
font-weight: bold ;
|
|
text-align: center }
|
|
|
|
div.attention, div.caution, div.danger, div.error, div.hint,
|
|
div.important, div.note, div.tip, div.warning {
|
|
margin: 2em ;
|
|
border: medium outset ;
|
|
padding: 1em }
|
|
|
|
div.attention p.admonition-title, div.caution p.admonition-title,
|
|
div.danger p.admonition-title, div.error p.admonition-title,
|
|
div.warning p.admonition-title {
|
|
color: red ;
|
|
font-weight: bold ;
|
|
font-family: sans-serif }
|
|
|
|
div.hint p.admonition-title, div.important p.admonition-title,
|
|
div.note p.admonition-title, div.tip p.admonition-title {
|
|
font-weight: bold ;
|
|
font-family: sans-serif }
|
|
|
|
div.dedication {
|
|
margin: 2em 5em ;
|
|
text-align: center ;
|
|
font-style: italic }
|
|
|
|
div.dedication p.topic-title {
|
|
font-weight: bold ;
|
|
font-style: normal }
|
|
|
|
div.figure {
|
|
margin-left: 2em }
|
|
|
|
div.footer, div.header {
|
|
font-size: smaller }
|
|
|
|
div.system-messages {
|
|
margin: 5em }
|
|
|
|
div.system-messages h1 {
|
|
color: red }
|
|
|
|
div.system-message {
|
|
border: medium outset ;
|
|
padding: 1em }
|
|
|
|
div.system-message p.system-message-title {
|
|
color: red ;
|
|
font-weight: bold }
|
|
|
|
div.topic {
|
|
margin: 2em }
|
|
|
|
h1.title {
|
|
text-align: center }
|
|
|
|
h2.subtitle {
|
|
text-align: center }
|
|
|
|
hr {
|
|
width: 75% }
|
|
|
|
ol.simple, ul.simple {
|
|
margin-bottom: 1em }
|
|
|
|
ol.arabic {
|
|
list-style: decimal }
|
|
|
|
ol.loweralpha {
|
|
list-style: lower-alpha }
|
|
|
|
ol.upperalpha {
|
|
list-style: upper-alpha }
|
|
|
|
ol.lowerroman {
|
|
list-style: lower-roman }
|
|
|
|
ol.upperroman {
|
|
list-style: upper-roman }
|
|
|
|
p.caption {
|
|
font-style: italic }
|
|
|
|
p.credits {
|
|
font-style: italic ;
|
|
font-size: smaller }
|
|
|
|
p.label {
|
|
white-space: nowrap }
|
|
|
|
p.topic-title {
|
|
font-weight: bold }
|
|
|
|
pre.address {
|
|
margin-bottom: 0 ;
|
|
margin-top: 0 ;
|
|
font-family: serif ;
|
|
font-size: 100% }
|
|
|
|
pre.line-block {
|
|
font-family: serif ;
|
|
font-size: 100% }
|
|
|
|
pre.literal-block, pre.doctest-block {
|
|
margin-left: 2em ;
|
|
margin-right: 2em ;
|
|
background-color: #eeeeee }
|
|
|
|
span.classifier {
|
|
font-family: sans-serif ;
|
|
font-style: oblique }
|
|
|
|
span.classifier-delimiter {
|
|
font-family: sans-serif ;
|
|
font-weight: bold }
|
|
|
|
span.interpreted {
|
|
font-family: sans-serif }
|
|
|
|
span.option-argument {
|
|
font-style: italic }
|
|
|
|
span.pre {
|
|
white-space: pre }
|
|
|
|
span.problematic {
|
|
color: red }
|
|
|
|
table {
|
|
margin-top: 0.5em ;
|
|
margin-bottom: 0.5em }
|
|
|
|
table.citation {
|
|
border-left: solid thin gray ;
|
|
padding-left: 0.5ex }
|
|
|
|
table.docinfo {
|
|
margin: 2em 4em }
|
|
|
|
table.footnote {
|
|
border-left: solid thin black ;
|
|
padding-left: 0.5ex }
|
|
|
|
td, th {
|
|
padding-left: 0.5em ;
|
|
padding-right: 0.5em ;
|
|
vertical-align: top }
|
|
|
|
th.docinfo-name, th.field-name {
|
|
font-weight: bold ;
|
|
text-align: left ;
|
|
white-space: nowrap }
|
|
|
|
h1 tt, h2 tt, h3 tt, h4 tt, h5 tt, h6 tt {
|
|
font-size: 100% }
|
|
|
|
tt, pre.literal-block, pre.doctest-block {
|
|
font-size: 115%;
|
|
line-height: 150% }
|
|
|
|
ul.auto-toc {
|
|
list-style-type: none }
|
|
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="document">
|
|
|
|
|
|
<div class="section" id="polymorphic-models-for-django">
|
|
<h1>Polymorphic Models for Django</h1>
|
|
<div class="section" id="quick-start-docs-contributing">
|
|
<h2>Quick Start, Docs, Contributing</h2>
|
|
<ul class="simple">
|
|
<li><a class="reference external" href="#good-for">What is django_polymorphic good for?</a></li>
|
|
<li><a class="reference external" href="http://bserve.webhop.org/django_polymorphic/DOCS.html#quickstart">Quickstart</a>, or the complete <a class="reference external" href="http://bserve.webhop.org/django_polymorphic/DOCS.html">Installation and Usage Docs</a></li>
|
|
<li><a class="reference external" href="http://groups.google.de/group/django-polymorphic/topics">Release Notes, News and Discussion</a> (Google Group) or <a class="reference external" href="http://bserve.webhop.org/django_polymorphic/CHANGES.html">Changelog</a></li>
|
|
<li>Download from <a class="reference external" href="http://github.com/bconstantin/django_polymorphic">GitHub</a> or <a class="reference external" href="http://bitbucket.org/bconstantin/django_polymorphic">Bitbucket</a>, or as <a class="reference external" href="http://github.com/bconstantin/django_polymorphic/tarball/master">TGZ</a> or <a class="reference external" href="http://github.com/bconstantin/django_polymorphic/zipball/master">ZIP</a></li>
|
|
<li>Improve django_polymorphic, report issues, discuss, patch or fork (<a class="reference external" href="http://github.com/bconstantin/django_polymorphic">GitHub</a>, <a class="reference external" href="http://bitbucket.org/bconstantin/django_polymorphic">Bitbucket</a>, <a class="reference external" href="http://groups.google.de/group/django-polymorphic/topics">Group</a>, <a class="reference external" href="http://github.com/bconstantin/django_polymorphic/tree/master/setup.py">Mail</a>)</li>
|
|
</ul>
|
|
</div>
|
|
<div class="section" id="id1">
|
|
<span id="good-for"></span><h2>What is django_polymorphic good for?</h2>
|
|
<p>Let's assume the models <tt class="docutils literal">ArtProject</tt> and <tt class="docutils literal">ResearchProject</tt> are derived
|
|
from the model <tt class="docutils literal">Project</tt>, and let's store one of each into the database:</p>
|
|
<pre class="doctest-block">
|
|
>>> Project.objects.create(topic="Department Party")
|
|
>>> ArtProject.objects.create(topic="Painting with Tim", artist="T. Turner")
|
|
>>> ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
|
|
</pre>
|
|
<p>If we want to retrieve all our projects, we do:</p>
|
|
<pre class="doctest-block">
|
|
>>> Project.objects.all()
|
|
</pre>
|
|
<p>Using django_polymorphic, we simply get what we stored:</p>
|
|
<pre class="literal-block">
|
|
[ <Project: id 1, topic "Department Party">,
|
|
<ArtProject: id 2, topic "Painting with Tim", artist "T. Turner">,
|
|
<ResearchProject: id 3, topic "Swallow Aerodynamics", supervisor "Dr. Winter"> ]
|
|
</pre>
|
|
<p>Using vanilla Django, we get incomplete objects, which is probably not what we wanted:</p>
|
|
<pre class="literal-block">
|
|
[ <Project: id 1, topic "Department Party">,
|
|
<Project: id 2, topic "Painting with Tim">,
|
|
<Project: id 3, topic "Swallow Aerodynamics"> ]
|
|
</pre>
|
|
<p>It's very similar for ForeignKeys, ManyToManyFields or OneToOneFields.</p>
|
|
<p>In general, the effect of django_polymorphic is twofold:</p>
|
|
<p>On one hand it makes sure that model inheritance just works as you
|
|
expect, by simply ensuring that you always get back exactly the same
|
|
objects from the database you stored there - regardless how you access
|
|
them, making model inheritance much more "pythonic".
|
|
This can save you a lot of unpleasant workarounds that tend to
|
|
make your code messy, error-prone, and slow.</p>
|
|
<p>On the other hand, together with some small API additions to the Django
|
|
ORM, django_polymorphic enables a much more expressive and intuitive
|
|
programming style and also very advanced object oriented designs
|
|
that are not possible with vanilla Django.</p>
|
|
<p>Fortunately, most of the heavy duty machinery that is needed for this
|
|
functionality is already present in the original Django database layer.
|
|
Django_polymorphic adds a rather thin layer above that in order
|
|
to make real OO fully automatic and very easy to use.</p>
|
|
<p>There is a catch however, which applies to concrete model inheritance
|
|
in general: Current DBM systems like PostgreSQL or MySQL are not very
|
|
good at processing the required sql queries and can be rather slow in
|
|
many cases. Concrete benchmarks are forthcoming (please see
|
|
discussion forum).</p>
|
|
<p>For more information, please look at <a class="reference external" href="http://bserve.webhop.org/django_polymorphic/DOCS.html#quickstart">Quickstart</a> or at the complete
|
|
<a class="reference external" href="http://bserve.webhop.org/django_polymorphic/DOCS.html">Installation and Usage Docs</a> and also see the <a class="reference external" href="http://bserve.webhop.org/django_polymorphic/DOCS.html#restrictions">restrictions and caveats</a>.</p>
|
|
</div>
|
|
<div class="section" id="this-is-a-v1-0-beta-testing-release">
|
|
<h2>This is a V1.0 Beta/Testing Release</h2>
|
|
<p>The release contains a considerable amount of changes in some of the more
|
|
critical parts of the software. It's intended for testing and development
|
|
environments and not for production environments. For these, it's best to
|
|
wait a few weeks for the proper V1.0 release, to allow some time for any
|
|
potential problems to turn up (if they exist).</p>
|
|
<p>If you encounter any problems or have suggestions regarding the API or the
|
|
changes in this beta, please post them in the <a class="reference external" href="http://groups.google.de/group/django-polymorphic/topics">discussion group</a>
|
|
or open an issue on <a class="reference external" href="http://github.com/bconstantin/django_polymorphic">GitHub</a> or <a class="reference external" href="http://bitbucket.org/bconstantin/django_polymorphic">BitBucket</a> (or send me an email).</p>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="license">
|
|
<h1>License</h1>
|
|
<p>Django_polymorphic uses the same license as Django (BSD-like).</p>
|
|
</div>
|
|
<div class="section" id="api-changes-additions">
|
|
<h1>API Changes & Additions</h1>
|
|
<div class="section" id="november-11-2010-v1-0-api-changes">
|
|
<h2>November 11 2010, V1.0 API Changes</h2>
|
|
<div class="section" id="extra-queryset-method">
|
|
<h3>extra() queryset method</h3>
|
|
<p><tt class="docutils literal">.extra()</tt> has been re-implemented. Now it's polymorphic by
|
|
default and works (nearly) without restrictions (please see docs). This is a (very)
|
|
incompatible API change regarding previous versions of django_polymorphic.
|
|
Support for the <tt class="docutils literal">polymorphic</tt> keyword parameter has been removed.
|
|
You can get back the non-polymorphic behaviour by using
|
|
<tt class="docutils literal"><span class="pre">ModelA.objects.non_polymorphic().extra()</span></tt>.</p>
|
|
</div>
|
|
<div class="section" id="no-pretty-printing-of-querysets-by-default">
|
|
<h3>No Pretty-Printing of Querysets by default</h3>
|
|
<p>In order to improve compatibility with vanilla Django, printing quersets
|
|
(__repr__ and __unicode__) does not use django_polymorphic's pretty printing
|
|
by default anymore. To get the old behaviour when printing querysets,
|
|
you need to replace your model definition:</p>
|
|
<pre class="doctest-block">
|
|
>>> class Project(PolymorphicModel):
|
|
</pre>
|
|
<p>by:</p>
|
|
<pre class="doctest-block">
|
|
>>> class Project(PolymorphicModel, ShowFieldType):
|
|
</pre>
|
|
<p>The mixin classes for pretty output have been renamed:</p>
|
|
<blockquote>
|
|
<tt class="docutils literal">ShowFieldTypes, ShowFields, ShowFieldsAndTypes</tt></blockquote>
|
|
<p>are now:</p>
|
|
<blockquote>
|
|
<tt class="docutils literal">ShowFieldType, ShowFieldContent and ShowFieldTypeAndContent</tt></blockquote>
|
|
<p>(the old ones still exist for compatibility)</p>
|
|
</div>
|
|
<div class="section" id="pretty-printing-output-format-changed">
|
|
<h3>Pretty-Printing Output Format Changed</h3>
|
|
<p><tt class="docutils literal">ShowFieldContent</tt> and <tt class="docutils literal">ShowFieldTypeAndContent</tt> now
|
|
use a slightly different output format. If this causes too much trouble for
|
|
your test cases, you can get the old behaviour back (mostly) by adding
|
|
<tt class="docutils literal">polymorphic_showfield_old_format = True</tt> to your model definitions.
|
|
<tt class="docutils literal"><span class="pre">ShowField...</span></tt> now also produces more informative output for custom
|
|
primary keys.</p>
|
|
</div>
|
|
<div class="section" id="polymorphic-dumpdata">
|
|
<h3>polymorphic_dumpdata</h3>
|
|
<p>The <tt class="docutils literal">polymorphic_dumpdata</tt> management command is not needed anymore
|
|
and has been disabled, as the regular Django dumpdata command now automatically
|
|
works correctly with polymorphic models (for all supported versions of Django).</p>
|
|
</div>
|
|
<div class="section" id="running-the-test-suite-with-django-1-3">
|
|
<h3>Running the Test suite with Django 1.3</h3>
|
|
<p>Django 1.3 requires <tt class="docutils literal">python manage.py test polymorphic</tt> instead of
|
|
just <tt class="docutils literal">python manage.py test</tt>.</p>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="november-01-2010-v1-0-api-additions">
|
|
<h2>November 01 2010, V1.0 API Additions</h2>
|
|
<ul>
|
|
<li><p class="first"><tt class="docutils literal">.non_polymorphic()</tt> queryset member function added. This is preferable to
|
|
using <tt class="docutils literal"><span class="pre">.base_objects...</span></tt>, as it just makes the resulting queryset non-polymorphic
|
|
and does not change anything else in the behaviour of the manager used (while
|
|
<tt class="docutils literal">.base_objects</tt> is just a different manager).</p>
|
|
</li>
|
|
<li><p class="first"><tt class="docutils literal">.get_real_instances()</tt> has been elevated to an official part of the API.
|
|
It allows you to turn a queryset or list of base objects into a list of the real instances.
|
|
This is useful if e.g. you use <tt class="docutils literal"><span class="pre">ModelA.objects.non_polymorphic().extra(...)</span></tt> and then want to
|
|
transform the result to its polymorphic equivalent:</p>
|
|
<pre class="doctest-block">
|
|
>>> qs = ModelA.objects.all().non_polymorphic()
|
|
>>> real_objects = qs.get_real_instances()
|
|
</pre>
|
|
<p>is equivalent to:</p>
|
|
<pre class="doctest-block">
|
|
>>> real_objects = ModelA.objects.all()
|
|
</pre>
|
|
<p>Instead of <tt class="docutils literal">qs.get_real_instances()</tt>, <tt class="docutils literal">ModelA.objects.get_real_instances(qs)</tt> may be used
|
|
as well. In the latter case, <tt class="docutils literal">qs</tt> may be any list of objects of type ModelA.</p>
|
|
</li>
|
|
<li><p class="first"><tt class="docutils literal">translate_polymorphic_Q_object</tt> (see DOCS)</p>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
<div class="section" id="february-22-2010-installation-note">
|
|
<h2>February 22 2010, Installation Note</h2>
|
|
<p>The django_polymorphic source code has been restructured
|
|
and as a result needs to be installed like a normal Django App
|
|
- either via copying the "polymorphic" directory into your
|
|
Django project or by running setup.py. Adding 'polymorphic'
|
|
to INSTALLED_APPS in settings.py is still optional, however.</p>
|
|
<p>The file <cite>polymorphic.py</cite> cannot be used as a standalone
|
|
extension module anymore (as is has been split into a number
|
|
of smaller files).</p>
|
|
<p>Importing works slightly different now: All relevant symbols are
|
|
imported directly from 'polymorphic' instead from
|
|
'polymorphic.models':</p>
|
|
<pre class="literal-block">
|
|
# new way
|
|
from polymorphic import PolymorphicModel, ...
|
|
|
|
# old way, doesn't work anymore
|
|
from polymorphic.models import PolymorphicModel, ...
|
|
</pre>
|
|
</div>
|
|
<div class="section" id="january-26-2010-database-schema-change">
|
|
<h2>January 26 2010: Database Schema Change</h2>
|
|
<p>The update from January 26 changed the database schema (more info in the <a class="reference external" href="http://github.com/bconstantin/django_polymorphic/commit/c2b420aea06637966a208329ef7ec853889fa4c7">commit-log</a>).
|
|
Sorry for any inconvenience. But this should be the final DB schema now.</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|