pealco pealco - 1 year ago 69
Python Question

In the MVC pattern, what goes in the model and what goes in the view?

I'm using the MVC pattern to design some data analysis software (in
Python). I'm not sure whether some functions should go in the model or the controller.

The way I've designed it, the user supplies the program a configuration file which contains the parameters for analysis. The program parses this file to find out what data files to look at and what kinds of calculations to do.

  • Should the function(s) that do this configuration file parsing be in the model or the controller?

  • Is there a standard way to organize things in Python when using MVC? I've made the model, controller, and view into separate classes. The controller class contains instances of the model and view classes.

  • What kind of communication should there be between the model and the view? The way I'm structuring things is so that the controller takes stuff from the model and passes it to the view. The two only communicate through the controller.

Answer Source

On your first point, the parsing should go in the model if it's part of the model class; for instance if you have a Configuration model, then the functions to load up configuration belong in the Model. If it's more of a service (e.g. I'm going to import this file in, and then something else is going to do the mappings) then it should be in the Controller. Without knowing more details it's kind of hard to say but that's the general rule that I follow.

I'm not sure on your second point since I'm not familiar with Python but the typical approach is to separate out your folder structure into Models/Controllers/Views and such, however I'm only familiar with MVC as it relates to web applications so I might be off the mark since your program seems to be a desktop app.

Your third point is exactly right. The model should not talk to the view; the controller communicates to the model and retrieves the information the View needs, and gives the view just that. I guess in essence the view might be working with the Model, but it doesn't know anything about the model (e.g. if it comes from a database, configuration file, xml, whatever)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download