如何隐藏many2one字段中的所有项目。我想只允许用户创建新的付款条件。
我们在account
中的account_invoice.py
模块中包含此代码:
class AccountInvoice(models.Model):
_name = "account.invoice"
payment_term_id = fields.Many2one('account.payment.term',
string='Payment Terms',
oldname='payment_term',
readonly=True,
states={'draft': [('readonly', False)]})
在account_invoice_view.xml
我们有:
<field name="payment_term_id"
options="{'no_create': True}"
attrs="{'invisible': [('payment_term_id','=',False)]}"/>
我尝试了这段代码{'no_open':True}
,但它没有用。
答案 0 :(得分:1)
如果要隐藏列表中的所有元素,请尝试添加始终为False
的域:
<field name="id" />
<field name="payment_term_id"
domain="[('id', '=', -1)]"
attrs="{'invisible': [('payment_term_id','=',False)]}"/>
options="{'no_create': True}"
如果我理解的话,你会得到你想要的反面
答案 1 :(得分:1)
我认为你会发现这个解决方案对你有好处:
建议在模型中添加一个字段,此字段为many2many字段
包含在当前记录中创建的payment_term_id
列表。
# this field is for technical purpose
create_payment_terms_ids = fields.Many2many(co_model='account.payment.term',
relation='created_payment_rel',
column1= 'invoice_id',
column2= 'paymen_id',
string='Created payment terms')
在此之后使用onchange方法来跟踪创建的付款并添加新的付款 此字段的付款条件
@api.onchange('payment_term_id')
def onchange_payment_terms(self):
""" keep track of created payment from the current
invoice and show only them in drop down list."""
if self.payment_term_id:
# keep list of old ids here
payment_ids = self.create_payment_terms_ids and self.create_payment_terms_ids.ids or []
# check if list payment is empty this means that this is the first created payment
# second check if the selected payment is a new payment that is created
# if one of the two is true add the selected value to the list of payment
if not self.create_payment_terms_ids or self.payment_term_id.id not in payment_ids:
payment_ids.append(self.payment_term_id.id)
self.create_payment_terms_ids = [(4, self.payment_term_id.id)]
# if list is not empty we change the domain to show only payment that exist in the list
# else we use a special domain to show empty set.
domain = payment_ids and [('payment_term_id', 'in', payment_ids)] or [('id', '=', -1)]
return {'domain': {'payment_term_id': domain}}
在您的视图中添加新字段visible = "1"
您不是用户可以看到它的值。
你需要将它放在视图中,因为你需要它在onchange事件中的值,如果你没有把它放在你的many2many字段将永远是空的。
<field name="create_payment_terms_ids" invisible="1"/>
并删除options="{'no_create':False}"
这将删除下拉列表中的创建和编辑选项,但您不希望这样。
注意:当您处于开发阶段时,请移除invisible="1"
以查看many2many字段是否包含您拥有的付款列表
创建。