This question has been flagged
3 Replies
5108 Views

Ok, i'm trying to create a list of product for a bill. When I save my bill show this error.

Integrity Error

The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set

[object with reference: Product - product.product]

This is my code:

my.py

producto_ids = fields.Many2one('product.product', 'Lista de Productos')
producto_box = fields.One2many('openacademy.factura', 'producto_ids', 'Order Lines')

my.xml

<field name="producto_box">

<tree editable="bottom">

<field name="producto_ids"/>

</tree>

</field>

Image:

https://db.tt/0iKfjOGG

Please help, what is missing?

Avatar
Discard

If you want to post code, please do it a a more descriptive manner. If the lines you quote in my.py is sequenced exactly like that, no wonder that it would fail. Also, my.xml does not have the context of what model this view is for.

Author

You're right (sorry for the comment in this, I need 50 karma to comment your answer) I try to obtain products in 'producto_box' but I try to do the same as module 'Sale' in 'Sale Order'. With Many2many I only obtain one product, and I need obtain more products of the same product.

@Juan Many2many allow you to have multiple entries, just like One2many. Many2one only allow you to have one entry. The reason I propose to use Many2many is that you may want to a product to multiple bills as well. If you specify it as One2many, then one product can only be linked to only one bill. @Med Said: it is true that in the official OpenERP most of the many2one fields are named with _id, but the naming for many2many and one2many are more varied: Invoice lines in account.invoice is invoice_line, sale order lines is order_line in sale.order, Physical Inventory lines is inventory_line_id in stock.inventory.

Author

Thanks very much, I edit my answer.

Best Answer

Why does the bill model (openacademy.factura) has a one2many field to itself using 'product_ids'?  This may be the cause of it.  The producto_box = fields.One2many('openacademy.factura', 'producto_ids', 'Order Lines') should be in product.product model, if I presume it correctly.

Or are you trying to get openacademy.factura to have several products in the producto_box?  If so, you should define producto_box = fields.Many2many('product.product', 'Order Lines') and remove the producto_ids = fields.Many2one('product.product', 'Lista de Productos').

Avatar
Discard
Author Best Answer

I resolve the problem, thanks Ivan and Med Said, thanks for the help and remind.

model.py

class Facturacion(models.Model):
    _name = 'openacademy.factura'

    numeroFactura = fields.Char(string="No. Factura")
    serieFactura = fields.Char(string="Serie")
    fechaFactura = fields.Date(string="Fecha")
    cantidadFactura = fields.Float('Cantidad')
    sin_iva = fields.Integer(string="Sin Iva", readonly=True)
    iva = fields.Integer(string="Con Iva", readonly=True)
    totales = fields.Integer(string="Totales", readonly=True)
    balance = fields.Integer(string="Balance", readonly=True)

#keys
    empresa_ids = fields.Many2one('openacademy.empresa', 'Empresa', store=True)
    producto_box = fields.One2many('openacademy.detalle', 'name', 'Order Lines')
    pago_ids = fields.Many2one('openacademy.pago', 'Tipo de Pago', store=True)
    partner_ids = fields.Many2one('res.partner', 'Cliente', store=True)

class DetalleFactura(models.Model):
    _name = "openacademy.detalle"

    name = fields.Char(string="Titulo")
    producto_id = fields.Many2one('product.product', 'Producto')
    precio = fields.Float(string="Precio")

my.xml

<record model="ir.ui.view" id="facturasform_list">
            <field name="name">Form View</field>
            <field name="model">openacademy.factura</field>
            <field name="arch" type="xml">
                <form string="Factura Form">
                    <sheet>
                        <group string="Factura">
                            <h2>No.<field name="numeroFactura" class="oe_inline"/></h2>
                            <h2 class="oe_right">Serie<field name="serieFactura" class="oe_inline oe_right"/></h2>
                            <br />
                            <br />
                            <h3 class="oe_left">Nombre de la Empresa:</h3><br />
                            <h3><field name="empresa_ids" class="oe_right"/></h3><h3 class="oe_left">Fecha:   <field name="fechaFactura" class="oe_inline oe_right"/>   </h3>
                            <br />
                            <br />
                            <h3 class="oe_left">Cliente<field name="partner_ids" class="oe_inline" domain="[('customer','=',True)]"/>    </h3><br />
                            <!--<field name="sales_ids"/>-->
                        </group>
                        
                        <group string="Opciones de Pago">
                            <field name="pago_ids"/>
                            
                        </group>
                        <notebook>
                            <page string="Compras">
                                    <group string="Compras">
                                        <field name="producto_box">
                                            <tree editable="bottom">
                                                <field name="producto_id"/>
                                                <field name="precio"/>
                                            </tree>
                                        </field>
                                        <group class="oe_subtotal_footer">
                                            <field name="sin_iva"/>
                                            <field name="iva"/>
                                            <field name="totales" class="oe_subtotal_footer_separator"/>
                                            <field name="balance" style="margin-top: -5px"/>
                                        </group>
                                    </group>
                            </page>
                        </notebook>
                    </sheet>
                </form>
            </field>
        </record>

I have only been two weeks learning, thank you very much.

Image of results: https://db.tt/7zDYHOfT

Avatar
Discard
Best Answer

In OpenERP: by convention, many2one fields end with '_id' and many2many or one2many end with '_ids'.

Avatar
Discard