One2many And Many 2one In Odoo 14

One2many in odoo

These are just mappings, You will mostly find them on any framework which uses a relational database. One2Many: Object A can have one or many of objects B (E.g A person can have many cars). Relationship: A -> B = One -> Many = One2Many.

 

Now we can add a One2many fields in our enzapps.estimate model

step 1:

You can create a field with type One2many under your class ,and  here we need another class for one manyfields declaration.

syntax as follows:

<field name> = fields.One2many(comodel='<comodel name>’,'<child_Model_name>’, ‘connector_field_id)

 

python code:

from odoo import models, fields

Here we can create another class for one2many

class ChildModel(models.Model):

_name = ‘child.model’

 

connecting_field_id = fields.Many2one(‘parent.model’, string=”Connector”)

child_field_1 = fields.Char(string=”Child Field 1″)

child_field_2 = fields.Integer(string=”Child Field 2″)

 

Note: Here we need one Many2one field to connect with our parent class

 in practical case:

from odoo import models,fields,api


class EnzappasEstimate(models.Model):
    _name='enzapps.estimate'

    p_type = fields.Many2one('res.partner')
    location = fields.Char()
    address = fields.Text()
    furnishing = fields.Selection(selection=[('yes', 'YES'), ('no', 'NO')])
    expected_date = fields.Date()
    created_date = fields.Datetime()
    currency = fields.Selection(selection=[('usd', 'USD'), ('inr', 'INR')])
    area_of_building = fields.Float()
    budget = fields.Integer()

    order_lines = fields.One2many('enzapps.estimate.line', 'connection')




class enzappsEstimateLineLines(models.Model):
        _name = 'enzapps.estimate.line'

        connection = fields.Many2one('enzapps.estimate')
        product_id = fields.Many2one('product.product', 'Product')
        quantity = fields.Float()
        unit_price = fields.Float()
        total_price = fields.Float()

        # price_subtotal = fields.Monetary()
        @api.onchange('quantity', 'unit_price')
        def total(self):
            self.total_price = self.quantity * self.unit_price


Step 2: Then you can create corresponding xml files with notebook, 
   syntax as follows:
 After closing </group> you can mension this code
<notebook>
    <page string="any name you can mension  here">
        <field name="one2many_field_name">
            <tree string="Anything you can add" editable="bottom">
                <field name="<field_name>"/>
                <field name="<field_name>"/>   
            </tree>
        </field>
    </page>
</notebook>

In practical case:
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<!-- To show fields-->
    <record id="enzapps_contract_tree_view" model="ir.ui.view">
        <field name="name">enzapps.estimate</field>
        <field name="model">enzapps.estimate</field>
        <field name="arch" type="xml">
            <tree>
                          <field name="p_type"/>
                          <field name="address"/>
                          <field name="furnishing"/>
                          <field name="expected_date"/>
                          <field name="created_date"/>
                          <field name="location"/>
                          <field name="currency"/>
                          <field name="area_of_building"/>
                          <field name="area_of_building"/>
                          <field name="budget"/>
            </tree>
        </field>
    </record>
  <record id="enzapps_contract_form_id" model="ir.ui.view">
      <field name="name">estimate.enzapps.form</field>
      <field name="model">enzapps.estimate</field>
      <field name="arch" type="xml">
          <form>
              <sheet>
                  <group>
                      <group>

                          <field name="p_type"/>
                          <field name="address"/>
                          <field name="furnishing"/>
                          <field name="expected_date"/>
                          <field name="created_date"/>
                      </group>
                      <group>
                          <field name="location"/>
                          <field name="currency"/>
                          <field name="area_of_building"/>
                          <field name="area_of_building"/>
                          <field name="budget"/>
                      </group>
                  </group>
<!--                  This for One2many fields-->
                  <notebook>
                      <page string="Estimate">
                          <field name="order_lines">
                              <tree string="Statement lines" editable="bottom">
                                  <field name="product_id"/>
                                  <field name="quantity"/>
                                  <field name="unit_price"/>
                                  <field name="total_price"/>
                              </tree>
                          </field>
                      </page>
                  </notebook>

              </sheet>
          </form>
      </field>
  </record>

    <!--    To show action menu-->
   <record id="enzapps_contact_action" model="ir.actions.act_window">
        <field name="name">Estimate Order</field>
        <field name="res_model">enzapps.estimate</field>
        <field name="view_mode">tree,form</field>
        <field name="help" type="html">
            <p class="oe_view_nocontent_create">
                No contract available
            </p>
        </field>
    </record>
<!--    To show menu-->
    <menuitem id="enzapps_contract"
              name="Enzapps Contract"
              sequence="1"
              action="enzapps_contact_action"/>

</odoo>

Now you can add in manifest file  then run the module it shows like this:



                                          MANY2ONE IN ODOO 14


A many2one field is used to link the current object to another object which acts as a parent
for this object. In simple terms, when we can show the records of another model and link it
with the current child model using a many2one relation. 

In Our form enzapps.estimate we can add one Many2one field name as  amount_payment_term

in py we can add product

amount_payment_term= fields.Many2one('account.payment.term','Amount Payment')

Then you can add in xml file, 
syntax as follows:
<record id="enzapps_contract_form_id" model="ir.ui.view">
    <field name="name">estimate.enzapps.form</field>
    <field name="model">enzapps.estimate</field>
    <field name="arch" type="xml">
        <form>
            <sheet>
                <group>
                        <field name="amount_payment_term"/>
                </group>
            </sheet>
       </form>
     </form>
</record>


The shows like this:



To know more about us

Leave a Reply

Your email address will not be published.