我正在运行一段简单的代码来打开generate_invoice视图,如果我在for和if语句下缩进它,它就不能工作。有人知道我错过了什么吗?
以下是我的代码
目标:代码工作是打开generate_invoice视图,如果在付款结构中有任何条目,for循环检查是否有任何帐户,如果有,则返回视图,如果没有引发验证错误。
class res_student(models.Model):
_name = 'res.student'此代码不工作,没有错误。
@api.multi
@api.depends('payment_structure')
def generate_invoice(self):
for x in self:
for rec in x.payment_structure:
if rec.account == False:
raise ValidationError("Please define a payment structure to Generate Invoice")
else:
form = self.env['generate.invoice']
id = self.id
record = form.create({'student_id': id,
'journal': x.journal.id,
'payment_type': x.payment_account.id})
return {'name': "Generate Invoice",
'type': 'ir.actions.act_window',
'res_model': 'generate.invoice',
'view_id': False,
'view_type': 'form',
'view_mode': 'form',
'res_id' : record.id,
'target': 'new',
'domain': '[]'}如果我删除了for和If语句,则可以正常工作。
@api.multi
@api.depends('payment_structure')
def generate_invoice(self):
form = self.env['generate.invoice']
id = self.id
record = form.create({'student_id': id,
'journal': x.journal.id,
'payment_type': x.payment_account.id})
return {'name': "Generate Invoice",
'type': 'ir.actions.act_window',
'res_model': 'generate.invoice',
'view_id': False,
'view_type': 'form',
'view_mode': 'form',
'res_id' : record.id,
'target': 'new',
'domain': '[]'}
#accounting
journal = fields.Many2one('account.journal')
payment_account = fields.Many2one('nominal.account')
payed = fields.Float(compute="get_payment")
discount = fields.Float(compute="get_discount")
payments = fields.One2many('invoice.line','student_id')
payment_structure = fields.One2many('payment.structure','student_id')
class payment_structure(models.Model):
_name = "payment.structure"
student_id = fields.Many2one('res.student')
account = fields.Many2one('nominal.account')
qty = fields.Integer()
unit = fields.Selection([('once','Once'),('month','Month')])
price = fields.Float()
class generate_invoice(models.TransientModel):
_name = 'generate.invoice'
student_id = fields.Many2one('res.student')
month = fields.Integer(string="Months")
payment_type = fields.Many2one('nominal.account', related="student_id.payment_account", )
payment_structure = fields.One2many(related="student_id.payment_structure")
journal = fields.Many2one('account.journal')发布于 2016-03-28 15:07:25
你可以参考销售订单,
您可以在其中找到“创建并查看发票”按钮。
从那里你可能会得到解决方案,因为我检查了这段代码和你的代码有一些不同。所以请试试这个。祝好运。
发布于 2016-03-30 11:04:40
如果在generate_invoice函数中删除for循环,将会出现错误,因为变量'x‘未定义。
请告诉我们generate_invoice函数是什么时候被调用的,以及其中包含了self。
https://stackoverflow.com/questions/36224579
复制相似问题