/README.html

https://bitbucket.org/bconstantin/django_polymorphic/ · HTML · 411 lines · 353 code · 58 blank · 0 comment · 0 complexity · 7c8912b120166c068ab818def4e42cf6 MD5 · raw file

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.6: http://docutils.sourceforge.net/" />
  7. <title></title>
  8. <style type="text/css">
  9. h1, h2, h3, h4,
  10. #table-of-contents
  11. {
  12. color: #47c;
  13. }
  14. h1 { padding-top: 15px; }
  15. h2 { padding-top: 10px; }
  16. h3 { padding-top: 7px; }
  17. a:hover { border-bottom: 1px solid #0066cc; }
  18. a {color: #0066cc; text-decoration: none;}
  19. li {
  20. padding-top: 5px;
  21. padding-bottom: 5px;
  22. }
  23. tt {
  24. color: #080;
  25. }
  26. blockquote tt {
  27. color: #000
  28. }
  29. .first {
  30. margin-top: 0 }
  31. .last {
  32. margin-bottom: 0 }
  33. /*
  34. a.toc-backref {
  35. text-decoration: none ;
  36. color: black }
  37. */
  38. dd {
  39. margin-bottom: 0.5em }
  40. div.abstract {
  41. margin: 2em 5em }
  42. div.abstract p.topic-title {
  43. font-weight: bold ;
  44. text-align: center }
  45. div.attention, div.caution, div.danger, div.error, div.hint,
  46. div.important, div.note, div.tip, div.warning {
  47. margin: 2em ;
  48. border: medium outset ;
  49. padding: 1em }
  50. div.attention p.admonition-title, div.caution p.admonition-title,
  51. div.danger p.admonition-title, div.error p.admonition-title,
  52. div.warning p.admonition-title {
  53. color: red ;
  54. font-weight: bold ;
  55. font-family: sans-serif }
  56. div.hint p.admonition-title, div.important p.admonition-title,
  57. div.note p.admonition-title, div.tip p.admonition-title {
  58. font-weight: bold ;
  59. font-family: sans-serif }
  60. div.dedication {
  61. margin: 2em 5em ;
  62. text-align: center ;
  63. font-style: italic }
  64. div.dedication p.topic-title {
  65. font-weight: bold ;
  66. font-style: normal }
  67. div.figure {
  68. margin-left: 2em }
  69. div.footer, div.header {
  70. font-size: smaller }
  71. div.system-messages {
  72. margin: 5em }
  73. div.system-messages h1 {
  74. color: red }
  75. div.system-message {
  76. border: medium outset ;
  77. padding: 1em }
  78. div.system-message p.system-message-title {
  79. color: red ;
  80. font-weight: bold }
  81. div.topic {
  82. margin: 2em }
  83. h1.title {
  84. text-align: center }
  85. h2.subtitle {
  86. text-align: center }
  87. hr {
  88. width: 75% }
  89. ol.simple, ul.simple {
  90. margin-bottom: 1em }
  91. ol.arabic {
  92. list-style: decimal }
  93. ol.loweralpha {
  94. list-style: lower-alpha }
  95. ol.upperalpha {
  96. list-style: upper-alpha }
  97. ol.lowerroman {
  98. list-style: lower-roman }
  99. ol.upperroman {
  100. list-style: upper-roman }
  101. p.caption {
  102. font-style: italic }
  103. p.credits {
  104. font-style: italic ;
  105. font-size: smaller }
  106. p.label {
  107. white-space: nowrap }
  108. p.topic-title {
  109. font-weight: bold }
  110. pre.address {
  111. margin-bottom: 0 ;
  112. margin-top: 0 ;
  113. font-family: serif ;
  114. font-size: 100% }
  115. pre.line-block {
  116. font-family: serif ;
  117. font-size: 100% }
  118. pre.literal-block, pre.doctest-block {
  119. margin-left: 2em ;
  120. margin-right: 2em ;
  121. background-color: #eeeeee }
  122. span.classifier {
  123. font-family: sans-serif ;
  124. font-style: oblique }
  125. span.classifier-delimiter {
  126. font-family: sans-serif ;
  127. font-weight: bold }
  128. span.interpreted {
  129. font-family: sans-serif }
  130. span.option-argument {
  131. font-style: italic }
  132. span.pre {
  133. white-space: pre }
  134. span.problematic {
  135. color: red }
  136. table {
  137. margin-top: 0.5em ;
  138. margin-bottom: 0.5em }
  139. table.citation {
  140. border-left: solid thin gray ;
  141. padding-left: 0.5ex }
  142. table.docinfo {
  143. margin: 2em 4em }
  144. table.footnote {
  145. border-left: solid thin black ;
  146. padding-left: 0.5ex }
  147. td, th {
  148. padding-left: 0.5em ;
  149. padding-right: 0.5em ;
  150. vertical-align: top }
  151. th.docinfo-name, th.field-name {
  152. font-weight: bold ;
  153. text-align: left ;
  154. white-space: nowrap }
  155. h1 tt, h2 tt, h3 tt, h4 tt, h5 tt, h6 tt {
  156. font-size: 100% }
  157. tt, pre.literal-block, pre.doctest-block {
  158. font-size: 115%;
  159. line-height: 150% }
  160. ul.auto-toc {
  161. list-style-type: none }
  162. </style>
  163. </head>
  164. <body>
  165. <div class="document">
  166. <div class="section" id="polymorphic-models-for-django">
  167. <h1>Polymorphic Models for Django</h1>
  168. <div class="section" id="quick-start-docs-contributing">
  169. <h2>Quick Start, Docs, Contributing</h2>
  170. <ul class="simple">
  171. <li><a class="reference external" href="#good-for">What is django_polymorphic good for?</a></li>
  172. <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>
  173. <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>
  174. <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>
  175. <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>
  176. </ul>
  177. </div>
  178. <div class="section" id="id1">
  179. <span id="good-for"></span><h2>What is django_polymorphic good for?</h2>
  180. <p>Let's assume the models <tt class="docutils literal">ArtProject</tt> and <tt class="docutils literal">ResearchProject</tt> are derived
  181. from the model <tt class="docutils literal">Project</tt>, and let's store one of each into the database:</p>
  182. <pre class="doctest-block">
  183. &gt;&gt;&gt; Project.objects.create(topic=&quot;Department Party&quot;)
  184. &gt;&gt;&gt; ArtProject.objects.create(topic=&quot;Painting with Tim&quot;, artist=&quot;T. Turner&quot;)
  185. &gt;&gt;&gt; ResearchProject.objects.create(topic=&quot;Swallow Aerodynamics&quot;, supervisor=&quot;Dr. Winter&quot;)
  186. </pre>
  187. <p>If we want to retrieve all our projects, we do:</p>
  188. <pre class="doctest-block">
  189. &gt;&gt;&gt; Project.objects.all()
  190. </pre>
  191. <p>Using django_polymorphic, we simply get what we stored:</p>
  192. <pre class="literal-block">
  193. [ &lt;Project: id 1, topic &quot;Department Party&quot;&gt;,
  194. &lt;ArtProject: id 2, topic &quot;Painting with Tim&quot;, artist &quot;T. Turner&quot;&gt;,
  195. &lt;ResearchProject: id 3, topic &quot;Swallow Aerodynamics&quot;, supervisor &quot;Dr. Winter&quot;&gt; ]
  196. </pre>
  197. <p>Using vanilla Django, we get incomplete objects, which is probably not what we wanted:</p>
  198. <pre class="literal-block">
  199. [ &lt;Project: id 1, topic &quot;Department Party&quot;&gt;,
  200. &lt;Project: id 2, topic &quot;Painting with Tim&quot;&gt;,
  201. &lt;Project: id 3, topic &quot;Swallow Aerodynamics&quot;&gt; ]
  202. </pre>
  203. <p>It's very similar for ForeignKeys, ManyToManyFields or OneToOneFields.</p>
  204. <p>In general, the effect of django_polymorphic is twofold:</p>
  205. <p>On one hand it makes sure that model inheritance just works as you
  206. expect, by simply ensuring that you always get back exactly the same
  207. objects from the database you stored there - regardless how you access
  208. them, making model inheritance much more &quot;pythonic&quot;.
  209. This can save you a lot of unpleasant workarounds that tend to
  210. make your code messy, error-prone, and slow.</p>
  211. <p>On the other hand, together with some small API additions to the Django
  212. ORM, django_polymorphic enables a much more expressive and intuitive
  213. programming style and also very advanced object oriented designs
  214. that are not possible with vanilla Django.</p>
  215. <p>Fortunately, most of the heavy duty machinery that is needed for this
  216. functionality is already present in the original Django database layer.
  217. Django_polymorphic adds a rather thin layer above that in order
  218. to make real OO fully automatic and very easy to use.</p>
  219. <p>There is a catch however, which applies to concrete model inheritance
  220. in general: Current DBM systems like PostgreSQL or MySQL are not very
  221. good at processing the required sql queries and can be rather slow in
  222. many cases. Concrete benchmarks are forthcoming (please see
  223. discussion forum).</p>
  224. <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
  225. <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>
  226. </div>
  227. <div class="section" id="this-is-a-v1-0-beta-testing-release">
  228. <h2>This is a V1.0 Beta/Testing Release</h2>
  229. <p>The release contains a considerable amount of changes in some of the more
  230. critical parts of the software. It's intended for testing and development
  231. environments and not for production environments. For these, it's best to
  232. wait a few weeks for the proper V1.0 release, to allow some time for any
  233. potential problems to turn up (if they exist).</p>
  234. <p>If you encounter any problems or have suggestions regarding the API or the
  235. 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>
  236. 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>
  237. </div>
  238. </div>
  239. <div class="section" id="license">
  240. <h1>License</h1>
  241. <p>Django_polymorphic uses the same license as Django (BSD-like).</p>
  242. </div>
  243. <div class="section" id="api-changes-additions">
  244. <h1>API Changes &amp; Additions</h1>
  245. <div class="section" id="november-11-2010-v1-0-api-changes">
  246. <h2>November 11 2010, V1.0 API Changes</h2>
  247. <div class="section" id="extra-queryset-method">
  248. <h3>extra() queryset method</h3>
  249. <p><tt class="docutils literal">.extra()</tt> has been re-implemented. Now it's polymorphic by
  250. default and works (nearly) without restrictions (please see docs). This is a (very)
  251. incompatible API change regarding previous versions of django_polymorphic.
  252. Support for the <tt class="docutils literal">polymorphic</tt> keyword parameter has been removed.
  253. You can get back the non-polymorphic behaviour by using
  254. <tt class="docutils literal"><span class="pre">ModelA.objects.non_polymorphic().extra()</span></tt>.</p>
  255. </div>
  256. <div class="section" id="no-pretty-printing-of-querysets-by-default">
  257. <h3>No Pretty-Printing of Querysets by default</h3>
  258. <p>In order to improve compatibility with vanilla Django, printing quersets
  259. (__repr__ and __unicode__) does not use django_polymorphic's pretty printing
  260. by default anymore. To get the old behaviour when printing querysets,
  261. you need to replace your model definition:</p>
  262. <pre class="doctest-block">
  263. &gt;&gt;&gt; class Project(PolymorphicModel):
  264. </pre>
  265. <p>by:</p>
  266. <pre class="doctest-block">
  267. &gt;&gt;&gt; class Project(PolymorphicModel, ShowFieldType):
  268. </pre>
  269. <p>The mixin classes for pretty output have been renamed:</p>
  270. <blockquote>
  271. <tt class="docutils literal">ShowFieldTypes, ShowFields, ShowFieldsAndTypes</tt></blockquote>
  272. <p>are now:</p>
  273. <blockquote>
  274. <tt class="docutils literal">ShowFieldType, ShowFieldContent and ShowFieldTypeAndContent</tt></blockquote>
  275. <p>(the old ones still exist for compatibility)</p>
  276. </div>
  277. <div class="section" id="pretty-printing-output-format-changed">
  278. <h3>Pretty-Printing Output Format Changed</h3>
  279. <p><tt class="docutils literal">ShowFieldContent</tt> and <tt class="docutils literal">ShowFieldTypeAndContent</tt> now
  280. use a slightly different output format. If this causes too much trouble for
  281. your test cases, you can get the old behaviour back (mostly) by adding
  282. <tt class="docutils literal">polymorphic_showfield_old_format = True</tt> to your model definitions.
  283. <tt class="docutils literal"><span class="pre">ShowField...</span></tt> now also produces more informative output for custom
  284. primary keys.</p>
  285. </div>
  286. <div class="section" id="polymorphic-dumpdata">
  287. <h3>polymorphic_dumpdata</h3>
  288. <p>The <tt class="docutils literal">polymorphic_dumpdata</tt> management command is not needed anymore
  289. and has been disabled, as the regular Django dumpdata command now automatically
  290. works correctly with polymorphic models (for all supported versions of Django).</p>
  291. </div>
  292. <div class="section" id="running-the-test-suite-with-django-1-3">
  293. <h3>Running the Test suite with Django 1.3</h3>
  294. <p>Django 1.3 requires <tt class="docutils literal">python manage.py test polymorphic</tt> instead of
  295. just <tt class="docutils literal">python manage.py test</tt>.</p>
  296. </div>
  297. </div>
  298. <div class="section" id="november-01-2010-v1-0-api-additions">
  299. <h2>November 01 2010, V1.0 API Additions</h2>
  300. <ul>
  301. <li><p class="first"><tt class="docutils literal">.non_polymorphic()</tt> queryset member function added. This is preferable to
  302. using <tt class="docutils literal"><span class="pre">.base_objects...</span></tt>, as it just makes the resulting queryset non-polymorphic
  303. and does not change anything else in the behaviour of the manager used (while
  304. <tt class="docutils literal">.base_objects</tt> is just a different manager).</p>
  305. </li>
  306. <li><p class="first"><tt class="docutils literal">.get_real_instances()</tt> has been elevated to an official part of the API.
  307. It allows you to turn a queryset or list of base objects into a list of the real instances.
  308. 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
  309. transform the result to its polymorphic equivalent:</p>
  310. <pre class="doctest-block">
  311. &gt;&gt;&gt; qs = ModelA.objects.all().non_polymorphic()
  312. &gt;&gt;&gt; real_objects = qs.get_real_instances()
  313. </pre>
  314. <p>is equivalent to:</p>
  315. <pre class="doctest-block">
  316. &gt;&gt;&gt; real_objects = ModelA.objects.all()
  317. </pre>
  318. <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
  319. as well. In the latter case, <tt class="docutils literal">qs</tt> may be any list of objects of type ModelA.</p>
  320. </li>
  321. <li><p class="first"><tt class="docutils literal">translate_polymorphic_Q_object</tt> (see DOCS)</p>
  322. </li>
  323. </ul>
  324. </div>
  325. <div class="section" id="february-22-2010-installation-note">
  326. <h2>February 22 2010, Installation Note</h2>
  327. <p>The django_polymorphic source code has been restructured
  328. and as a result needs to be installed like a normal Django App
  329. - either via copying the &quot;polymorphic&quot; directory into your
  330. Django project or by running setup.py. Adding 'polymorphic'
  331. to INSTALLED_APPS in settings.py is still optional, however.</p>
  332. <p>The file <cite>polymorphic.py</cite> cannot be used as a standalone
  333. extension module anymore (as is has been split into a number
  334. of smaller files).</p>
  335. <p>Importing works slightly different now: All relevant symbols are
  336. imported directly from 'polymorphic' instead from
  337. 'polymorphic.models':</p>
  338. <pre class="literal-block">
  339. # new way
  340. from polymorphic import PolymorphicModel, ...
  341. # old way, doesn't work anymore
  342. from polymorphic.models import PolymorphicModel, ...
  343. </pre>
  344. </div>
  345. <div class="section" id="january-26-2010-database-schema-change">
  346. <h2>January 26 2010: Database Schema Change</h2>
  347. <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>).
  348. Sorry for any inconvenience. But this should be the final DB schema now.</p>
  349. </div>
  350. </div>
  351. </div>
  352. </body>
  353. </html>