Goodnight,
I am trying to insert into a table, values from a query, but I have an error with the one2many fields, how should I get the line_id.
thanks,
2020-04-05 02:15:40,541 1 INFO odoo2 werkzeug: 192.168.99.1 - - [05/Apr/2020 02:15:40] "POST /web/dataset/call_kw/wizard.comprobantes.detalle.formato/read HTTP/1.1" 200 -
el id de cabecera es: 124
2020-04-05 02:15:40,618 1 ERROR odoo2 odoo.sql_db: bad query: b"\n INSERT INTO \n wizard_comprobantes_detalle (linea_id,create_uid, create_date,number, product_id, price_total)\n select \n 124 as linea_id,\n 1 AS create_uid,\n NOW() AS create_date,\n f.number,df.product_id,df.price_total from account_invoice_line df inner join account_invoice f on f.id=df.id\n where f.date_invoice>='2020-02-10' and f.date_invoice<='2020-04-05'\n "
ERROR: insert or update on table "wizard_comprobantes_detalle" violates foreign key constraint "wizard_comprobantes_detalle_linea_id_fkey"
DETAIL: Key (linea_id)=(124) is not present in table "wizard_comprobantes_detalle_formato".
2020-04-05 02:15:40,630 1 INFO odoo2 werkzeug: 192.168.99.1 - - [05/Apr/2020 02:15:40] "POST /web/dataset/call_button HTTP/1.1" 200 -
2020-04-05 02:15:40,654 1 INFO odoo2 werkzeug: 192.168.99.1 - - [05/Apr/2020 02:15:40] "POST /web/dataset/call_kw/wizard.comprobantes.detalle.formato/read HTTP/1.1" 200 -
2020-04-05 02:15:47,069 1 INFO ? odoo.service.server: Initiating shutdown
2020-04-05 02:15:47,070 1 INFO ? odoo.service.server: Hit CTRL-C again or send a second signal to force the shutdown.
2020-04-05 07:15:52,453 1 INFO ? odoo: Odoo version 11.0-20190322
2020-04-05 07:15:52,459 1 INFO ? odoo: Using configuration file at /etc/odoo/odoo.conf
2020-04-05 07:15:52,460 1 INFO ? odoo: addons paths: ['/var/lib/odoo/addons/11.0', '/mnt/extra-addons', '/usr/lib/python3/dist-packages/odoo/addons']
2020-04-05 07:15:52,460 1 INFO ? odoo: database: odoo@db:5432
2020-04-05 07:15:52,505 1 INFO ? odoo.service.server: HTTP service (werkzeug) running on 536c66b2c3d1:8069
2020-04-05 07:15:52,969 1 INFO ? odoo.addons.base.ir.ir_actions_report: Will use the Wkhtmltopdf binary at /usr/local/bin/wkhtmltopdf
2020-04-05 02:16:08,076 1 INFO ? odoo.http: HTTP Configuring static files
2020-04-05 02:16:08,197 1 INFO odoo2 odoo.modules.loading: loading 1 modules...
2020-04-05 02:16:08,282 1 INFO odoo2 odoo.modules.loading: 1 modules loaded in 0.08s, 0 queries
2020-04-05 02:16:08,833 1 INFO odoo2 odoo.modules.loading: loading 63 modules...
2020-04-05 02:16:11,534 1 INFO odoo2 odoo.modules.loading: 63 modules loaded in 2.70s, 0 queries
2020-04-05 02:16:15,168 1 INFO odoo2 odoo.modules.loading: Modules loaded.
2020-04-05 02:16:15,193 1 INFO odoo2 odoo.addons.base.ir.ir_http: Generating routing map
el id de cabecera es: 134
2020-04-05 02:16:15,491 1 ERROR odoo2 odoo.sql_db: bad query: b"\n INSERT INTO \n wizard_comprobantes_detalle (linea_id,create_uid, create_date,number, product_id, price_total)\n select \n 134 as linea_id,\n 1 AS create_uid,\n NOW() AS create_date,\n f.number,df.product_id,df.price_total from account_invoice_line df inner join account_invoice f on f.id=df.id\n where f.date_invoice>='2020-02-10' and f.date_invoice<='2020-04-05'\n "
ERROR: insert or update on table "wizard_comprobantes_detalle" violates foreign key constraint "wizard_comprobantes_detalle_linea_id_fkey"
DETAIL: Key (linea_id)=(134) is not present in table "wizard_comprobantes_detalle_formato".
2020-04-05 02:16:15,513 1 INFO odoo2 werkzeug: 192.168.99.1 - - [05/Apr/2020 02:16:15] "POST /web/dataset/call_button HTTP/1.1" 200 -
2020-04-05 02:16:15,549 1 INFO odoo2 werkzeug: 192.168.99.1 - - [05/Apr/2020 02:16:15] "POST /web/dataset/call_kw/wizard.comprobantes.detalle.formato/read HTTP/1.1" 200 -
2020-04-05 02:16:17,334 1 INFO ? odoo.addons.bus.models.bus: Bus.loop listen imbus on db postgres
class WizardInvoiceLineReport(models.TransientModel):
_name = 'wizard.comprobantes.detalle.formato'
fecha_inicio = fields.Date("Fecha de Inicio")
fecha_fin = fields.Date("Fecha Fin")
company_currency_id = fields.Many2one('res.currency')
company_id = fields.Many2one('res.company', string='Company', readonly=True, default=lambda self: self.env.user.company_id)
linea=fields.One2many('wizard.comprobantes.detalle','linea_id')
def btn_generate_xlsx(self):
report_obj = self.env.ref("report_comprobantes_efact.linea_report_formato_xlsx")
return report_obj.report_action([],{"fecha_inicio":self.fecha_inicio,"fecha_fin":self.fecha_fin})
def imprimir(self):
self._sql_get_line_for_report()
return self.env.ref('report_comprobantes_efact.linea_report_formato_pdf').report_action(self)
def _sql_get_line_for_report(self):
self.env['account.invoice.line'].check_access_rights('read')
obj=self.env['wizard.comprobantes.detalle']
for li in self.browse(self.ids):
obj.create({
'linea_id':li.id
})
os.system("echo '%s'"%(str("el id de cabecera es: " + str(self.linea.id))))
query = """
INSERT INTO
wizard_comprobantes_detalle (linea_id,create_uid, create_date,number, product_id, price_total)
select
%s as linea_id,
%s AS create_uid,
NOW() AS create_date,
f.number,df.product_id,df.price_total from account_invoice_line df inner join account_invoice f on f.id=df.id
where f.date_invoice>=%s and f.date_invoice<=%s
"""
params = [
self.linea.id,
self.env.uid,
self.fecha_inicio,self.fecha_fin,
]
self.env.cr.execute(query, tuple(params))
return True
class WizardInvoiceDetalleReport(models.TransientModel):
_name = 'wizard.comprobantes.detalle'
linea_id=fields.Many2one('wizard.comprobantes.detalle.formato')
number=fields.Char("Numero")
product_id=fields.Integer("Id Producto")
price_total=fields.Float("Precio Total Linea")