0

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.

Community
  • 1
  • 1
  • i know there's an accepted answer...but hope you know there is a `fields.Selection` type of field. or a you looking to build a pure website? – danidee Jul 15 '16 at 08:40

1 Answers1

1

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.

Nolan Zandi
  • 111
  • 9
  • Your answer is probably incorrect because I didn't need controllers when I did the same exact thing but for another field that wasn't a drop-down and it worked. But if you insist that I need to change something in the controllers then please edit your question to include what I should do in the controller or put it as a comment. –  Jul 14 '16 at 17:05
  • There may be a way to post directly to a model, but this is the way that's done in the PHP example in your question using Odoo and it's how I've saved form data in Odoo as well. Using t-att-value is using Odoos Qweb template language and those values can only be called if it's generated in your controller. https://www.odoo.com/documentation/8.0/reference/qweb.html – Nolan Zandi Jul 14 '16 at 17:20
  • What I should do in the controller? –  Jul 14 '16 at 17:30
  • See my recent edit in the answer that shows how to post to a route, declare the route in the controller, and then use the ORM to take the data (sent to the controller as delivery_time) and create a new line in the database. I wasn't sure the name of your module or model so I just guessed but those areas will have to match your actual models. – Nolan Zandi Jul 14 '16 at 17:35
  • i loved your answer Odoo keep surprising me can you post useful link how to create a website and save data from the webpage to the model – Charif DZ Jul 14 '16 at 20:32
  • Odoo is amazing. I've built production level applications with dozens of users for clients on it. It's almost as dynamic as Ruby on Rails as a framework and it comes with very powerful business software in addition. I'm not sure what you mean by post a link to create a website. Unfortunately, their documentation is not as detailed as it should be and I had to learn a lot of my own. But the Odoo.com tutorials were useful to start: http://www.odoo.com/documentation/9.0/index.html Maybe let me know if you're struggling with any particular step and I can help out. – Nolan Zandi Jul 14 '16 at 21:36