Accessing parent fields / Passing field to child relation

Hi, I’m trying to obtain fields of the parent in on_change_with however I keep getting None. I’ve tried the answer from On_change on a selection field inside a one2many field but I still can’t seem to obtain the value of the parent field.

In my parent.py, I have the following fields

    pharmacy = fields.Many2One(
        'party.party', 'Pharmacy')

    child= fields.One2Many(
        'prescriptions.child', 'name', 'Prescriptions')

In my child.py

    pharmacy_location = fields.Integer("Pharmacy Location")

@fields.depends('DependingOnChangeField','_parent_pharmacy','pharmacy_location')
def on_change_with_pharmacy_location(self):
        return self._parent_pharmacy.id

When I create a new child and modify DependingOnChangeField, this results in an exception as _parent_pharmacy does not exist in the model, and I checked pharmacy and pharmacy_location in self and they are both None as well

Am I missing something here or is there any other ways to obtain parent field values? It doesn’t necessarily have to be in on_change_with, as my main purpose is to obtain the parent field values when I create a new child or edit existing child, not to update the value of pharmacy_location.

Thanks.

After ‘_parent’, you have to specify the many2one field of the child class which is linking it with the parent, so you have to put something like ('name', '_parent_name.pharmacy') assuming that name is the field that points to the parent class.

The return should be just:
return self.name.pharmacy.id

Thanks for the answer. Now I’m able to obtain the id of the pharmacy.

On top of that, just checking if is it possible for me to get the fields of pharmacy other than id? Currently I’m only able to get the id of the pharmacy

For example can I directly get self.name.pharmacy.location.id?

With the pharmacy.id I would be able to search for the location.id in DB, but I wonder if I can reduce this redundancy by directly obtaining the location.id in just one step.

Maybe I’m wrong but I would say yes, can you check if the pharmacy you are using to test it have a location?

I tried but other than name.pharmacy.id, I can’t get any of the other fields of name.pharmacy :frowning:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.