首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >代码在for循环python odoo 8下不起作用

代码在for循环python odoo 8下不起作用
EN

Stack Overflow用户
提问于 2016-03-26 01:12:11
回答 2查看 346关注 0票数 0

我正在运行一段简单的代码来打开generate_invoice视图,如果我在for和if语句下缩进它,它就不能工作。有人知道我错过了什么吗?

以下是我的代码

目标:代码工作是打开generate_invoice视图,如果在付款结构中有任何条目,for循环检查是否有任何帐户,如果有,则返回视图,如果没有引发验证错误。

代码语言:javascript
复制
class res_student(models.Model):
_name = 'res.student'

此代码不工作,没有错误。

代码语言:javascript
复制
@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语句,则可以正常工作。

代码语言:javascript
复制
@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')
EN

回答 2

Stack Overflow用户

发布于 2016-03-28 15:07:25

你可以参考销售订单,

您可以在其中找到“创建并查看发票”按钮。

从那里你可能会得到解决方案,因为我检查了这段代码和你的代码有一些不同。所以请试试这个。祝好运。

票数 0
EN

Stack Overflow用户

发布于 2016-03-30 11:04:40

如果在generate_invoice函数中删除for循环,将会出现错误,因为变量'x‘未定义。

请告诉我们generate_invoice函数是什么时候被调用的,以及其中包含了self。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36224579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档