Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Integrity Error One2many with Many2one. [Solved]

By
Juan Carlos
on 12/17/14, 6:57 PM 1,204 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?

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.

Ivan
on 12/17/14, 9:21 PM

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 Carlos
on 12/18/14, 10:42 AM

@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.

Ivan
on 12/18/14, 12:05 PM

Thanks very much, I edit my answer.

Juan Carlos
on 12/18/14, 12:42 PM
1

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 12/18/14, 10:19 AM

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').

0
Juan Carlos
On 12/18/14, 10:05 AM

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

0

Med Said BARA

--Med Said BARA--
2664
| 5 5 7
Algeria
--Med Said BARA--
Med Said BARA
On 12/18/14, 12:57 PM

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)

Stats

Asked: 12/17/14, 6:57 PM
Seen: 1204 times
Last updated: 3/16/15, 8:10 AM