odoohtml-selectodoo-9odoo-viewodoo-website

How to save data from a drop-down list in odoo


In one of my pages of my odoo 9 website I have a drop down list(using select tag in xml view), how can I save the option that the user chooses?

Code:

<select type="text" name="delivery_time" class="form-control" t-att-value="website_sale_order.delivery_time">
    <option value="1">8 AM - 10 AM</option>
    <option value="2">10 AM - 12 PM</option>
    <option value="3">12 PM - 2 PM</option>
    <option value="4">2 PM - 4 PM</option>
    <option value="5">4 PM - 6 PM</option>
</select

Model:

class odss_sale_order(models.Model):
    _inherit = "sale.order"

    delivery_time = fields.Char()

As you can see I have already tried doing stuff in the model so it works but I cant seem to get it, any idea why it doesn't work?

EDIT: I am looking for an answer similar to this one but for Odoo instead of PHP.


Solution

  • If you need to interact with models within your webpage, you're probably going to want to create a controller, which can contain python code and allows your web page to interact with your models (within your module or across Odoo in general). On a simple level, a controller can go in your module main folder (ie /mymodule/my_template_controller.py) and be declared in your init.py file (import my_template_controller). You can then set a route in the controller that matches your template id and create forms on the XML page that post to the controller, allowing python code to be run and lines of data to be saved or recalled from your models.

    Specifically, the code to create a new line in a model, using the ORM in a controller, is, first the form:

    <openerp>
    <data>
    <template id="sales_order" name="Sales Order">
    <t t-call="website.layout">
        <form action='/odss/sales_order/' method="POST" enctype="multipart/form-data">
        <select type="text" name="delivery_time" class="form-control">
            <option value="1">8 AM - 10 AM</option>
            <option value="2">10 AM - 12 PM</option>
            <option value="3">12 PM - 2 PM</option>
            <option value="4">2 PM - 4 PM</option>
            <option value="5">4 PM - 6 PM</option>
        </select>
        </form>
    </t>
    </template>
    </data>
    </openerp>
    

    Then in your controller (call it whatever and declare it init.py):

    class SalesOrderController(http.Controller):
        @http.route('/odss/sales_order/', auth='user', website=True, csrf=False)
        def create_sales_order(self, delivery_time):
    
            odss_sales_order = http.request.env['sale.orders']
            new_so           = odss_sales_order.create({
                              'delivery_Time': delivery_time,
                               }) 
    
           return http.request.render('module_name.sales_order', {
        }) 
    

    Odoo does have some decent documentation that explains the process. The first is how to set up the website, using a controller as the intermediary between your view and your model:

    http://www.odoo.com/documentation/9.0/howtos/website.html

    The second is how to use what is called the Object Relational Model (ORM), which is the Odoo specific code that allows for calling and saving data to the database from the controller. Once you have a controller connected to your webpage (via the route), then you can post data to the route on your form and save it to your database in the controller using the ORM:

    https://www.odoo.com/documentation/9.0/reference/orm.html

    Odoo modules can be constructed like any other Model/Controller/View application such as Ruby on Rails and it's actually quite functional.