Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The separation between model/view is intended to decouple presentation from data. Your initial description of a polymorphic model hierarchy for <code>Question</code> is indeed a valid approach.</p> <p>What you really want to do here is consider using Django's model inheritance to handle data hierarchies i.e. have:</p> <pre><code>BaseQuestion &lt;- FreeQuestion, MultipleChoiceQuestion, SlidingScaleQuestion etc. </code></pre> <p>Then you can build a <code>BaseQuestionView</code> that know how to display a <code>BaseQuestion</code> (for instance render the Question string, style it and what not) and using the same principle construct:</p> <pre><code>BaseQuestionView &lt;- FreeQuestionView, MultipleChoiceQuestionView, SlidingScaleQuestionView </code></pre> <p>You can make the BaseQuestionView abstract as to pull all <code>BaseQuestion</code> model instances from the DB and invoke abstract <code>render_question</code> method that is implemented in each of <code>FreeQuestionView</code>, <code>MultipleChoiceQuestionView</code>, <code>SlidingScaleQuestionView</code> subclasses. Thus <code>FreeQuestionView</code> knows its working with a <code>FreeQuestion</code> model and only implements how to render widget for the answer (textfield). <code>MultipleChoiceQuestionView</code> would only implement how to render radio boxes etc.</p> <p>In other words it is almost exactly what you presented in your first case, except the rendering implementation lies in the View classes not Model classes.</p> <p>Same principle can be applied when you want to render the same class of objects in different ways.</p> <hr> <p>With model inheritance you can access any subclass of the base instance with dot notation i.e.: <code>question.freequestion</code>. This will return to you FreeQuestion instance associated with base instance or raise <code>Question.DoesNotExist</code> if that's not its class.</p> <p>Using class-based views you can add Mixins that can render your question differently depending on the weather this is a FreeQuestion, MultipleChoiceQuestion, using python's MRO pattern, or you can subclass them.</p> <p>As far as I know there is no automatic way for Django to make the correlation between inherited models and inherited views, you have to make the mapping yourself.</p> <p>Perhaps the easiest approach is to explicit request all instances matching your initial Question QuerySet related to FreeQuestions, MultipleChoiceQuestions, etc. individually and cast them in the list before feeding it to your main renderer which will then go through a <code>map[question.__class__]</code> to find the renderer method from the mixin. Alternatively you can just keep the question type in the base class as to avoid having to deal with class mappings and let the DB help you in this regard.</p> <p>However, normally you don't want your model behavior to dynamically change for the same class (which is what you are effectively doing with BaseQuestion). This is especially true when designing with REST in mind, as you want explicit URLs to map to explicit concrete not abstract types.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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