首页 文章

将记录集传递给控制器Odoo中的模板中的对象

提问于
浏览
1

我在Odoo网站上有一个表单,其中包含国家和州选择字段 . 此表单由控制器填充,我将所有州和国家/地区的记录集作为dict中的值传递,其中州和国家/地区为键 . 现在对于选择字段,它使用t-foreach通过状态循环以提供选项和值 . 现在,在选择国家时,我需要能够过滤掉该特定国家的状态 . 这是可能的,如果是的话怎么样?以下是我尝试过的一些代码:以下是最初加载表单的控制器,第二种方法是过滤掉状态

@http.route('/admission_applications', type='http', auth="public", website=True)
def application_form(self):
    cr, uid, context, registry = request.cr, request.uid, request.context, request.registry
    classes=[('lkg',"LKG"),('ukg',"UKG"),('1', "1st"), ('2', "2nd"),('3', "3rd"),('4', "4th"),('5', "5th"),('6', "6th"),('7', "7th"),('8', "8th"),('9', "9th"),('10', "10th"),('11', "11th"),('12', "12th")]
    gender=[('male', "Male"), ('female', "Female")]
    orm_country = http.request.env['res.country']
    state_orm = http.request.env['res.country.state']
    countries = orm_country.search([])
    #countries = orm_country.browse(cr, SUPERUSER_ID, country_ids, context)
    states = state_orm.search([])
    #states = state_orm.browse(cr, SUPERUSER_ID, states_ids, context)
    return request.render("website_admission_application.admission_form", {'classes':classes, 'gender':gender, 'countries':countries, 'states':states})



@http.route('/action_get_states/<country_id>',type='http', auth="public", website=True)
def states_listing(self,country_id):
    states_info=[]     
    logging.error(country_id)
    states=http.request.env['res.country.state'].search([('country_id.id','=',country_id)])
    for state in states:
        states_info.append({'id':state.id, 'name':state.name})


    logging.error(states_info)      
    return states_info

接下来是模板相关代码:

<div t-attf-class="form-group form-field o_website_form_required_custom margin-0" t-if="countries">
    <div class="col-sm-12 col-md-6">
    <label class="control-label" for="country_id">Country</label>
    <select name="country_id" id="country_id" class="form-control" onchange="countryselection()" required="">
         <option value="">Country...</option>
         <t t-foreach="countries" t-as="country">
             <option t-att-value="country.id"><t t-esc="country.name"/></option>
         </t>
    </select>
    </div>
    <div class="col-sm-12 col-md-6">
        <label class="control-label" for="state_id">State</label>
         <select name="state_id" id="state_id" class="form-control" required="">
             <option value="">State...</option>
             <t t-foreach="states" t-as="state">
                 <option t-att-value="state.id"><t t-esc="state.name"/></option>
             </t>
         </select>
     </div> 
 </div>

js调用控制器方法来过滤状态是这样的:

function countryselection() {
    alert("hai");
    var country = document.getElementById('country_id').value;
    alert(country);
    $.ajax({
        type: "GET",
        url: "/action_get_states/"+country,
        success: function(data) {
            $("states").html(data);
        }
    });     
}

我知道这不是正确的方法,但我不知道如何将记录集返回到我最初已经加载表单的状态 .

1 回答

  • 1
    //This is just an example which I used on Odoo website.
    // Just make your proper changes by changing field names.    
    $(document).ready(function () {
    
            $('select[name=country_id]').change(function () {
            $('#state_id option').remove();
            var states = new Model('res.country.state').call('search_read',[[['country_id', '=', parseInt($('select[name=country_id]').val())]]]).then(function(result){
            for (var i=0; i<result.length; i++){
                $('#state_id').append($('<option>',
                     {
                        value: result[i].id,
                        text : result[i].name
                     }));
            }
            })
    
            });
    

相关问题