As an MVC framework, does web2py allows updating model from view?

97 views
Skip to first unread message

Mike Stephenson

unread,
Nov 9, 2017, 1:42:09 AM11/9/17
to web2py-users

Can model directly communicate with the model as depicted in the MVC flow diagram here?

Leonel Câmara

unread,
Nov 9, 2017, 7:47:42 AM11/9/17
to web2py-users
In the way shown in that diagram yes it can. You can make db(query).select() inside a view so you can actually get the data directly from the model. That is not usually recommended. 

Mike Stephenson

unread,
Nov 9, 2017, 7:50:17 AM11/9/17
to web2py-users
So in the same way, can it make updates in the db? And why is it not recommended in web2py if the diagram doesn't say so?

Leonel Câmara

unread,
Nov 9, 2017, 7:55:00 AM11/9/17
to web2py-users
It's not recommended because the logic in web2py is that you use the same controller for many different views (html, json, pdf, etc) so if you put the getting of data in the view you will have to replicate it in all the other views for that same controller. However you should use it where it makes sense, for instance, if it's data you are only going to need in that specific view.  
  
Yes in the same way you can actually make updates, that would be a very bad idea as you would be spreading business logic everywhere.

Mike Stephenson

unread,
Nov 9, 2017, 7:58:38 AM11/9/17
to web2py-users
Thanks

Anthony

unread,
Nov 9, 2017, 10:04:02 AM11/9/17
to web...@googlegroups.com
I suspect this model is intended to describe the original MVC pattern of event-driven desktop GUI apps rather than MVC as commonly implemented in server-side web frameworks. The view in particular in this diagram doesn't quite map to what web2py calls a view.

Note, some frameworks, such as Django and Flask, use different terminology for the controller and view. What web2py calls controllers, Django calls views, as they are functions/classes responsible for updating the model data or getting data from the model to be displayed to the user. What web2py calls views, Django simply calls templates. Django considers the framework itself (in particular, the logic that routes HTTP requests to views) as the "controller" part of the MVC architecture. For further explanation, see here and here.

The point is, there can be a lot of confusion about terminology. So, rather than ask whether web2py allows the "model" to be updated from the "view", it might be more productive if you explain exactly what you are trying to achieve (and why).

Anthony
Reply all
Reply to author
Forward
0 new messages