Capturing Form component attributes into a data structure

Anvil has the convention of calling item any generic item.

When an app manages trucks or crates I pass Truck or Crate objects to the form.

Sometimes the form’s item is the truck, sometimes the truck is inside the item dictionary because the form needs other info not related to the truck.

Sometimes I’m lazy and use self.item['truck_number'], sometimes (always) I do either self.truck = self.item or self.truck = self.item['truck'], depending on how I pass the info to the form, then I work with self.truck. More readable than self.item.

The above applies to forms used as repeating panel templates, where Anvil automatically populate self.item. In top level forms I don’t even use item. I pass the truck object as an argument of the __init__ at form creation time.

1 Like