Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
3 Vastaukset
16526 Näkymät

In the contact application we can see that every contact (partner model) has an address. That address if form by several fields (Street, Street 2, city, state, zip, country) How can a add those fields to a custom model. I net the code for both the model and the view


Avatar
Hylkää

You can either inherit/inherits the partner or just create those fields in your custom model.

Paras vastaus

Hi,
You can define the res.partner many2one field as follows in form view definition.

<field name="partner_id" widget="res_partner_many2one"
context="{'show_address': 1}"
options='{"always_reload": True}'/>

When you save the record. It will show the address without creating the separate fields for address fields.

Regards

Avatar
Hylkää
Paras vastaus

You can also reference or bring the appropriate fields using computed fields and then use the following code in your view

                            <div class="o_address_format">
                                <field name="street" placeholder="Street..." class="o_address_street" attrs="{'readonly': [('type', '=', 'contact'),('parent_id', '!=', False)]}"/>
                                <field name="street2" placeholder="Street 2..." class="o_address_street" attrs="{'readonly': [('type', '=', 'contact'),('parent_id', '!=', False)]}"/>
                                <field name="city" placeholder="City" class="o_address_city" attrs="{'readonly': [('type', '=', 'contact'),('parent_id', '!=', False)]}"/>
                                <field name="state_id" class="o_address_state" placeholder="State" options="{&quot;no_open&quot;: True}" attrs="{'readonly': [('type', '=', 'contact'),('parent_id', '!=', False)]}" context="{'country_id': country_id, 'zip': zip}"/>
                                <field name="zip" placeholder="ZIP" class="o_address_zip" attrs="{'readonly': [('type', '=', 'contact'),('parent_id', '!=', False)]}"/>
                                <field name="country_id" placeholder="Country" class="o_address_country" options="{&quot;no_open&quot;: True, &quot;no_create&quot;: True}" attrs="{'readonly': [('type', '=', 'contact'),('parent_id', '!=', False)]}"/>
                            </div>   

Avatar
Hylkää
Paras vastaus

hi, my solution for same purpose (to have address from contacts) is as below (thanks to Cybrosys Techno):

from odoo import models, fields, api


class Stores(models.Model):
    _name = 'tests.stores'
    _rec_name = 'name'
    _description = "Tests Stores"

    store_id = fields.Many2one('res.partner', string="Select Store", domain="[['category_id.name','ilike','store']]")
    # below line for address will have complete address from 'contacts'
    # after selected a record from contacts in above 'store_id' field
    address = fields.Text(string="Store Address", compute="_compute_address")


    # this function will get required fields and concatinate them in address field
    @api.depends('store_id')
    def _compute_address(self):
        for rec in self:
            res = [rec.store_id.street, rec.store_id.street2,
                   rec.store_id.city,
                   rec.store_id.zip]
            self.address = ', '.join(filter(bool, res))
Avatar
Hylkää
Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
4
tammik. 24
23096
2
tammik. 24
2410
0
elok. 23
2283
5
syysk. 21
17469
0
jouluk. 19
3879