# Accessing parent fields / Passing field to child relation

**URL:** https://discuss.tryton.org/t/accessing-parent-fields-passing-field-to-child-relation/3540
**Category:** Developer
**Created:** [December 11, 2020, 2:02am UTC](https://discuss.tryton.org/t/accessing-parent-fields-passing-field-to-child-relation/3540 "2020-12-11T02:02:56Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![enixzyrex](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@enixzyrex](https://discuss.tryton.org/u/enixzyrex)
#### Post date: [December 11, 2020, 2:02am UTC](https://discuss.tryton.org/t/accessing-parent-fields-passing-field-to-child-relation/3540/1 "2020-12-11T02:02:56Z")

</div>

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 - #2 by oscarQ](https://discuss.tryton.org/t/on-change-on-a-selection-field-inside-a-one2many-field/1552/2) but I still can’t seem to obtain the value of the parent field.

In my parent.py, I have the following fields

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

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

```

In my child.py

```auto
    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.

---

<div class="post-metadata">

### Author: ![oscarQ](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/oscarq/32/413_2.png) [@oscarQ](https://discuss.tryton.org/u/oscarQ)
#### Post date: [December 11, 2020, 6:50am UTC](https://discuss.tryton.org/t/accessing-parent-fields-passing-field-to-child-relation/3540/2 "2020-12-11T06:50:25Z")

</div>

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`

---

<div class="post-metadata">

### Author: ![enixzyrex](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@enixzyrex](https://discuss.tryton.org/u/enixzyrex)
#### Post date: [December 11, 2020, 8:24am UTC](https://discuss.tryton.org/t/accessing-parent-fields-passing-field-to-child-relation/3540/3 "2020-12-11T08:24:35Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![oscarQ](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/oscarq/32/413_2.png) [@oscarQ](https://discuss.tryton.org/u/oscarQ)
#### Post date: [December 11, 2020, 9:38am UTC](https://discuss.tryton.org/t/accessing-parent-fields-passing-field-to-child-relation/3540/4 "2020-12-11T09:38:07Z")

</div>

> [@enixzyrex](#):
>
> For example can I directly get `self.name.pharmacy.location.id`?

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

---

<div class="post-metadata">

### Author: ![enixzyrex](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@enixzyrex](https://discuss.tryton.org/u/enixzyrex)
#### Post date: [December 11, 2020, 9:46am UTC](https://discuss.tryton.org/t/accessing-parent-fields-passing-field-to-child-relation/3540/5 "2020-12-11T09:46:20Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)
#### Post date: [January 10, 2021, 9:46am UTC](https://discuss.tryton.org/t/accessing-parent-fields-passing-field-to-child-relation/3540/6 "2021-01-10T09:46:26Z")

</div>

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