Table of contents
1.
Introduction 🌼
2.
Workflow of Web2py 🤓
3.
What are Conditional Models ⭐
4.
Example of Conditional Models of Web2py ❓
5.
Conditional Models of Web2py commands 🛣️
6.
Frequently Asked Questions 🤹
6.1.
What is CRON in Web2py?
6.2.
In which file are scheduled events listed in web2py?
6.3.
Why is Web2py different from Django?
6.4.
What are the protocols that Web2py supports?
6.5.
What is RBAC in Web2py?
7.
Conclusion ✉️
Last Updated: Mar 27, 2024
Easy

Conditional Models of Web2py

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 🌼

Web2py works on the MVC concept, where the model is a source of information about our data. It keeps all the necessary fields and behaviors of the data we store. Each model represents a single database table.

Conditional Models of Web2py

Web2py treats models as conditional. Models of Web2py execute on controllers' requirements; because of this, it only processes the required tables at each request. So in this blog, we will know about those conditional models of Web2py deeply.

Workflow of Web2py 🤓

Workflow of Web2py
  • Components of the Models, Views, and Controller collectively make up the user web2py application.
     
  • The user sends the HTTP request to the server through the browser, and the server reaches back to the user with Model, Controller, and View to provide the essential information.
     
  • The arrows show the data transfer with the database engine.
     
  • The model creates the database connection and communicates with Controllerller. At the same time, Controllerller communicates with the View to display the data.
     
  • The Dispatcher schedules the requested URL at HTTP response to a function call in Controllerller. The output of this operation can either be a hash table or string.
     
  • View schedules the data. The View organizes the data into an HTML or XML page if the user requests an HTML or XML page.
     
  • Web2py supports HTML, XML, JSON, RSS, CSV, and RTF protocols.

What are Conditional Models

Conditional models are like other ordinary models of the MVC concept; the only difference is that the conditional models execute only on the need or demand of the controller. Conditionality is not the concept of models, but it is the concept of Web2py. Models of every project developed with the help of the web2py framework act like a conditional model. In the next section, examples of Conditional models in Web2py. 

Example of Conditional Models of Web2py

The below given example will help to understand more about conditional models of Web2py.

Suppose the user wants to get some data that are stored at "/app/cntrl/fun" where the app is the application, "cntrl" is the controller, and "fun" is the function the web2py will execute the below-given models:

applications/app/models/*.py
applications/app/models/cntrl/*.py
applications/app/models/cntrl/fun/*.py
You can also try this code with Online Python Compiler
Run Code

 

This is the default behavior of Web2py. We can also push the behavior of our own choice by:

“response.models_to_run this command.”

 

This command will execute the requested controller and call the requested function. It means at every request for the controller, all the top-level codes stored in the controller will execute. 

If a function returns a dictionary, only the view is called, and if the view is not available Web2py transfers the generic view to the user. In web2py Generic views are disabled by default, but the 'welcome' app contains a command "/models/db.py through which we can enable Generic views on localhost. "response.generic_patterns" can enable per extension type and per action. Generic views are development tools, and we should not use them in project development. 

We enlist all the actions in "response.generic_patterns," which we want to use a generic view. 

Conditional Models of Web2py commands 🛣️

Below is the use of some common functions used in Conditional Models of Web2py and their descriptions:

  • def index():
    return 'data': This function is part of the Conditional Models of Web2py that returns string data.
     
  • def index():
    return dict(key='value'): The output of this function will be a dictionary for a view. 
     
  • def index():
    return locals(): This function redirects the client to another page.
     
  • def index():
    redirect(URL('other_action'))” : This function returns the code of HTTP status other than “200 OK”.
     
  •  def index():
    raise HTTP(404): This function is a part of conditional models of Web2py that returns a helper.
    For example, let's take a FORM():
     
def index():
	return FORM(INPUT(_name='test'))” : This function is used for Ajax callbacks and components.

 

  • def index():
    return dict(form=SQLFORM.factory(Field('filed_name')).process()): This function returns dictionary. Dictionaries generated by the function may contain codes generated by helpers, including forms of a factory or forms depending on the database.
     
  • def download():
    return response.download(request, db): This function helps in streaming and downloading files from the database "server" with the help of the "response. download" helper.
     
  • def index():
    return response.stream('/tmp/foo.bar', request=request, attachment=True)” :This function of the conditional model of Web2py serves the arbitrary content using the "response. stream" helper.

Frequently Asked Questions 🤹

What is CRON in Web2py?

CRON is a function in Web2py that facilitates the Web2py by running the task within a specified time limit.

In which file are scheduled events listed in web2py?

Wep2py stores scheduled events in the scheduler.py file.

Why is Web2py different from Django?

Web2py is designed based on Django, so we can say that Web2py is more advanced than Django and is more compact and easy to learn.

What are the protocols that Web2py supports?

XML, XMLRPC, JSON, JSON RPC, AND RPC, RSS, CSV, and SOAP are the protocols which Web2py supports.

What is RBAC in Web2py?

RBAC is a compelling Role Based Access Control system restricting authorized users' access.

Conclusion ✉️

Model is a very important part of any project-based MVC controller. In this blog, we learned about conditional models of Web2py, why we call them conditional models of Web2py, what its commands are, and how it works.

Also read,

 

Refer to our guided paths on Coding Ninjas Studio to learn more about C programming, DSA, JavaScript, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!

Conclusion Image
Live masterclass