简体   繁体   English

我想根据Selection Field填充Many2one字段

[英]I want to populate Many2one field based on Selection Field

I want to populate Customer/Vendor Many2one field based on Selection Field which has two selection 1.Sale Order, 2.Purchase Order. 我想基于Selection Field填充Customer / Vendor Many2one字段,该字段有两个选择1.Sale Order,2.Purchase Order。 If it is Sale Order then Many2one field should have Customers from sale.order model and if it is Purchase Order then Many2one field should have Vendors from purchase.order model. 如果是销售订单,那么Many2one字段应该有来自sale.order模型的客户,如果是Purchase Order那么Many2one字段应该有来自purchase.order模型的供应商。

from odoo import models, fields, api, _ 来自odoo导入模型,字段,api,_

class ProductRejection(models.Model): class ProductRejection(models.Model):

_name = 'product.rejection'

pr_type_of_order = fields.Selection([('SO', 'Sale Order'), ('PO', 'Purchase Order')], string="Order Type")
pr_customer_name = fields.Many2one('sale.order',string="Vendor/Customer")
pr_order_no = fields.Many2one('sale.order',string="PO/SO No.")
pr_rejection_date = fields.Date("Date Of Rejection")
pr_product_name = fields.Many2one('product.product',string='Product Name')
pr_presentation = fields.Char("Presentation")
pr_reason = fields.Text("Reason Of Rejection")
pr_notes = fields.Text("Receiving Notes")
pr_status = fields.Selection([('new','New'),('confirm','Confirmed')],string="Status")

@api.onchange('pr_type_of_order')
def update_customer(self):
  result = {}
  if self.pr_type_of_order == 'SO':
    sale_customer_obj = self.env['sale.order']
    sale_customer_ids = sale_customer_obj.search(['partner_id'])
    for record in sale_customer_ids:
        cust_name = []
        cust_name.append(record.id)
        result = {'domain': {'pr_customer_name': [('id', 'in', cust_name)]}}
    return result
  else:
      if self.pr_type_of_order == 'PO':
        sale_customer_obj = self.env['purchase.order']
        sale_customer_ids = sale_customer_obj.search(['partner_id'])
        for record in sale_customer_ids:
            cust_name1 = []
            cust_name1.append(record.id)
            result = {'domain': {'pr_customer_name': [('id', 'in', cust_name1)]}}
        return result

You take m2o of purchase.order and sale.order Now from the selection field, give attrs over .xml file. 您可以从选择字段中选择m2o of purchase.ordersale.order 。将attrs放在.xml文件上。 In which try to hide/unhide other fields. 其中试图隐藏/取消隐藏其他字段。 Now from that m2o, you can easily get filter from customer / vendor. 现在从m2o,您可以轻松地从客户/供应商处过滤。 Now apart from that if you don't have any other usage of sale.order than just take m2o of 'res.partner' and set the domain over this fields on the base of selection field value. 现在除此之外,如果你没有任何其他使用sale.order而不仅仅是采取'res.partner'的m2o并在选择字段值的基础上在这些字段上设置域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM