Note that there are some explanatory texts on larger screens.

plurals
  1. POModel polymorphism and model-view separation
    primarykey
    data
    text
    <p>I'm encountering a dilemma of sorts while making my Django application, but I think the problem I'm encountering may apply to the MVC pattern generally. I'm making a <code>Question</code> model which can be used to construct quizzes or questionnaires. The <code>Question</code> base class would be a simple free response question. I'd like to support different types of questions such as multiple choice questions or sliding scale questions, and these would be subclasses of the <code>Question</code> base class with extra fields added such as an array of possible choices. I'd like to be able to extend my question models to support more types of questions in the future, and for that I can rely on polymorphism and pass objects of the <code>Question</code> type between the model layer and the view layer for all subclasses of <code>Question</code>.</p> <p>The problem I'm encountering is that the view has to know the type of question it has received in order to render it. If it gets a multiple choice question it needs to draw the radio choice widgets, etc. So now if I extend my models with more types of questions I have to add it to both the model and the view layers. This seems to defeat the point of polymorphism since the views receiving the <code>Question</code> objects would always have to know the subclass type of the questions received. I can get around this problem by delegating the responsibility of rendering the question back to the model. If the <code>Question</code> model has a virtual function called <code>render_question()</code> that its subclasses override then the view layer can call that function to get the right HTML to output without worrying about the type of question. But now I have the problem of having the HTML rendering code bound up with the model.</p> <p>Could there be a third solution that does not have either of the downsides of the solutions I've thought of? Or is this truly a dilemma about which one has to make a difficult decision?</p>
    singulars
    1. This table or related slice is empty.
    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.
    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