Data binding error : initiated attribute not found

Hi anvil people

My problem:
In the __init__ method of a form, I’ve created a dict self.agency (see code sample below).
In the form UI, i set a label data binding to self.agency['name'], which is a string variable.
Notice that it is created before the line of self.init_components(**properties).
However, i get Data Binding Errors :

Code Sample:

from .....MODULES import GLOBAL_VARIABLES

class my_class(my_classTemplate):
    def __init__(self, item, **properties):
        self.item = dict(item)
        self.agency = GLOBAL_VARIABLES.global_data['agencies'][self.item['agency_id']]
        # Set Form properties and Data Bindings.
        self.init_components(**properties)

My question:
Why is there this problem ?
The only way of it happening is if the data binding process first time initialization occurs BEFORE the form’s init_components call, hence explaining why it can’t see my self.agency variable ?
But is it the case ?
If yes, does it mean i can only use data binding with self.item ?

Thanks for your help !

What you’re trying to do is fine, so there’s something else going on. In your error message, it says alert_edit_booking has no attribute agency. The code you show is for my_class, so isn’t where you need self.agency set.

Without more context, or a clone link, I can’t really offer any more concrete advice.

Hi @jshaffstall

My bad, in the code sample i provided, i changed the name of the class to something general for clarity purposes, but i didn’t check the screenshot (huh looks like i’ll stop doing this from now on …)

The real name of the class (and the form) in my code is indeed alert_edit_booking.

Hope it helps understand the problem now…

You’ll probably need to provide a clone link to get any help, then, since when I try to recreate the problem in a new app it works fine.

Well i can’t share too much since it’s a commercialized app…
If anyone else can reproduce the issue, let me know…

I’m gonna contact the support if it can help…

Thanks

You should try to create a minimal example app that reproduces the problem. Quite often, when you create that minimal example app, you’ll realize what’s causing the original problem. And if you can create the minimal app to reproduce the problem, you can post a clone link to that.

3 Likes

Well

Thankfully I found the “problem”.

It was in fact described in here : https://anvil.works/docs/client/data-bindings.

In fact, setting self.item automatically initiates a data binding process (let’s call it process 0).

Since self.agency is set after self.item, the agency property is not defined yet during this process 0.
Hence the binding errors.

So, for those interested, call self.item only after you have defined the properties you need for data binding, like this in my example :

from .....MODULES import GLOBAL_VARIABLES

class my_class(my_classTemplate):
    def __init__(self, item, **properties):
        self.agency = GLOBAL_VARIABLES.global_data['agencies'][item['agency_id']]
        self.item = dict(item)
        # Set Form properties and Data Bindings.
        self.init_components(**properties)
1 Like