在odoo 12中如何在过滤方法之后设置字段many2many的值

时间:2019-03-29 14:44:07

标签: odoo-12

我有一个模型,在这个模型中:“类别”字段可以与许多项目相关。 如果字段“类别”已更改,项目已过滤,则需要设置field2的值,但我需要在field2中保持项目的过滤。这是树状视图的屏幕截图:enter image description here

这是python代码:

// VUEX

const state = {
  visitors: [],
  url: 'API URL',
  errors: []
}
const mutations = {
  ADD_VISITOR(state, response) {
    const data = response.data;
    data.Photos = [];
    state.visitors.data.push(data);
  },
}
const actions = {
  addVisitor: ({ commit }, insertion) => {
    return axios
      .post(state.url + 'api/visitor', {
        name: insertion.visitorName
      })
      .then(response => {
        commit('ADD_VISITOR', response);
      })
      .catch(error => state.errors.push(error.response.data.message));
    state.errors = [];
  },
}

// MY COMPONENT FROM WHERE THE ACTIONS ARE BEING DISPATCHED

<div ref="scroll" class="visitors-scroll">
  <ul v-if="visitors.data && visitors.data.length > 0" class="list-group visitors-panel">
    <!-- Displaying appVisitor component and sending data as a prop -->
    <app-visitor v-for="visitor in visitors.data" :key="visitor.id" :visitor="visitor"></app-visitor>
  </ul>
</div>

methods: {
  // Function that dispatches the "addVisitor" action to add a new visitor to the database
  newVisitor() {
    const insertion = {
      visitorName: this.name
    };
    if (insertion.visitorName.trim() == "") {
      this.errors.push("Enter a valid name!");
    } else {
      this.$store.dispatch("addVisitor", insertion)
        .then(() => {
           this.scrollDown();
         })
      this.name = "";
    }
    this.errors = [];
  },
  scrollDown() {
    this.$refs.scroll.scrollTop = this.$refs.scroll.scrollHeight;
  }
},

这是xml代码:

class class4(models.Model):
    _inherit = 'sale.order.line'
    categorie = fields.Many2one('module.categorie')
    item = fields.Many2one('module.item')
    # field1 = fields.Char(compute='change_domain')
    items = fields.Char(compute='change_domain')
    field2 = fields.Many2many('module.item')

    @api.one
    @api.onchange('categorie')
    def change_domain(self):
        print("call change_domain")
        res = {}
        list = []
        listChar = []
        records = self.env['module.categorieitem'].search([('name', '=', self.categorie.id)])
        for record in records:
            list.append(record.item.id)
        print('set items value')
        for record in self:
            record.items = list
            print("items list: ",record.items)
        res['domain'] = {'item': [('id', 'in', list)]}
        return res

这是表单视图的屏幕截图:

enter image description here

0 个答案:

没有答案