Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a couple approaches you can take to handling the <code>QuerySet</code>s of "mixed models" that you obtain when you perform this kind of query. A lot of it depends on your ultimate goal.</p> <p>The "simple and dumb" way, which I use a lot, is to manage the results with utility functions. If you plan to process the result of <code>Build.objects.filter(project="myproject")</code> in a template, for example, you could use custom template tags or filters to take special action. Assume in the below code <code>build_objects</code> contains the result of your filter():</p> <pre><code>{% for build in build_objects %} {% if build|is_custom_build %} &lt;p&gt;This is a custom build of custom type {{ build.custom_build.custom_type }}!&lt;/p&gt; {% endif %} &lt;p&gt;This is a custom build OR a standard build&lt;/p&gt; {% endfor %} </code></pre> <p>The obvious problem here is if you have numerous subclasses, writing template filters may be impractical or grow tedious. However, in my experience I usually have a half-dozen subclasses at most so this is not always a problem.</p> <p>You could also write a parameterized filter like so:</p> <pre><code>{% if build|is_of_buildtype:"custom_build" %} </code></pre> <p>With the filter code as follows:</p> <pre><code>def is_of_buildtype_filter(value, arg): if hasattr(value, arg): return True else: return False </code></pre> <p>This filter simply checks for the presence of the argument as an attribute on the <code>build</code> object (passed in as <code>value</code>). The argument string should be the name of the automatically generated <code>OneToOneField</code> that you want to detect, in this case <code>custom_build</code>.</p> <p>For view code, similar sorts of helper functions would work the same way but are even easier because you don't need to write custom filters or tags.</p> <p>This approach works in many cases, but there are more complicated scenarios where it might not be practical. Unfortunately, Django cannot natively provide you with a <code>QuerySet</code> that contains subclass instances when you perform operations on the base class (ie. a <code>QuerySet</code> that truly contains "mixed models"). You may require this in situations where processing the results with helper functions is not possible.</p> <p>I personally avoid these situations entirely, usually by rethinking my model design. But if that's not possible, there are many interesting attempts at solutions, like this <a href="http://lazypython.blogspot.com/2009/02/second-look-at-inheritance-and.html" rel="nofollow noreferrer">Inheritance MixIn</a>. There are also several Django snippets on the subject. Be aware, though, that almost any solution like this will be performance-limited.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload