1

enter image description hereI am new to Odoo customizing an currently stuck on a Many2one field. Following the Code Snippet I am working on:

This is my Python Code:

My One2many field:

field_contacts_customer_info = fields.One2many(
        'contacts.customer.information', 'another_id', string='Contacts for customer information')

My class:

class ContactsCustomerInformation(models.Model):
    _name = 'contacts.customer.information'
    _rec_name = 'name_contacts'

    name_contacts = fields.Many2one(
        'res.partner', string="Person", domain="[('is_company' , '=' , False)]")

    mail_contacts = fields.Char(
        related = 'name_contacts.email' ,string="Email")

    another_id = fields.Many2one('res.partner', string="AnotherID")

    @api.onchange('name_contacts')
    def onchange_name_contacts(self):
        if self.name_contacts:
            if self.name_contacts.email:
                self.mail_contacts = self.name_contacts.email

And my XML:

    <page name="contacts_customer_information" string="Contacts for customer information" attrs="{'invisible': [('is_company','=', False)]}">
                    <field name="field_contacts_customer_info">
                        <tree editable="bottom">
                            <field name="name_contacts"/>
                            <field name="mail_contacts" domain="[('is_company' , '=' , False)]"/>
                            <field name="another_id" invisible="1"/>
                            <field name="versuch" invisible="1"/>
                        </tree>
                            <form>
                                <group>
                                    <group>
                                        <field name="name_contacts" domain="[('is_company' , '=' , False)]"/>
                                    </group>
                                    <group>
                                        <field name="mail_contacts" domain="[('is_company' , '=' , False)]"/>
                                    </group>
                                </group>
                            </form>
                    </field>
                </page>

At the moment I have only one condition and that is that in the Many2one field no Companys but only Individuals are displayed. I also want to display only the Individuals that belong to the Company on whose view you are currently located. I know that I have to link this with an AND condition.

What is the condition that only the Individuals of the currently displayed Company are displayed ?

Thanks.

enter image description here

kayalotta
  • 53
  • 9

1 Answers1

0

I think you have the company field that related to res.partner.And then if you want to display individual that belong to this company ,try this

class ContactsCustomerInformation(models.Model):
    _name = 'contacts.customer.information'
    _rec_name = 'name_contacts'


        
    name_contacts = fields.Many2one(
        'res.partner', string="Person", domain=[('parent_id','=',another_id)])

    mail_contacts = fields.Char(
        related = 'name_contacts.email' ,string="Email")

    another_id = fields.Many2one('res.partner', string="AnotherID")

    @api.onchange('name_contacts')
    def onchange_name_contacts(self):
        if self.name_contacts:
            if self.name_contacts.email:
                self.mail_contacts = self.name_contacts.email
Neural
  • 370
  • 1
  • 11
  • Thanks for your answer. I have tried it like you suggested but I get an error that says "object has no attribute child_ids". But I dont really get why it says that, because res.partner has a attribute which is called child_ids. – kayalotta Nov 24 '21 at 07:54
  • @kayalotta. Can you share your code.So I can suggest you more. – Neural Nov 24 '21 at 08:32
  • shure, I have added my code – kayalotta Nov 24 '21 at 10:30
  • @kayalotta. You used versuch in domain but you make it invisible and not filled data for this field in one2many field.That why you got error. – Neural Nov 24 '21 at 10:36
  • I made it invisible, because I dont want to display a third field. I only want to display the two fields name_contacts and mail_contacts. I did not get why you added the field company ? – kayalotta Nov 24 '21 at 10:50
  • @kayalotta. I misunderstood your question. I edited my answer. – Neural Nov 24 '21 at 14:10
  • I tried your suggestion but the Many2One field name_contacts still shows me all the Individuals of all companys an not only the Individuals of the company on which view I am currently on(I think because I am set as Superuser right now). BUT: Which Individuals are shown in the Many2one field shouldnt depend on the user and to which company he belongs but in which companys view the user is on. For example when I am on the view of company Azure Interior I want the Many2one field to show me only Individuals from Azure Interior even if the user isnt a AzureInterior Individual – kayalotta Nov 25 '21 at 08:51
  • @kayalotta.check my answwer. – Neural Nov 25 '21 at 09:03
  • Now it shows me the same selection on every companys view. I always get "Your company",so the company which I am assignet to, and the individuals that belong to that company. – kayalotta Nov 25 '21 at 11:12
  • I have added a picture, so maybe it is easier to unterstand. In the picture I am on the view of Azure Interior and in my One2many field I would like to be able to select individuals from azure company only. But when I am on another companys view I want to only select from these companys individuals. – kayalotta Nov 25 '21 at 11:21
  • @kayalotta. I edited my answer. – Neural Nov 26 '21 at 01:50
  • I tried this and it gave me an internal server error saying "another_id" is not definded. – kayalotta Nov 29 '21 at 07:42
  • I also tried to put another_id in quotation marks but then the Many2one field displayed nothing – kayalotta Nov 29 '21 at 07:48
  • @kayalotta. Can you give me some testing environment with your code. So I can help easily. – Neural Nov 29 '21 at 07:58