gtalarico / pyairtable

Python Api Client for Airtable

Home Page:https://pyairtable.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LookupField referencing a LinkField returns id rather than entity

david-waterworth opened this issue · comments

I'm trying to generate a model from an existing airtable schema. I'm having trouble modelling a chain where a LookupField references a field in a linked table, where the referenced field is itself a LinkField to a third table.

i.e. in the schema below, Template.equipment_types does a lookup of the linked Category.equipment_types, and this field is itself linked to EquipmentType

If I materialise a Category, i.e. Category.from_id("XXX") , the equipment_types field is a list of EquipmentType

But if I materialise a Template, equipment_types is a list of str containing EquipmentType ids and I have perform an additional request to get the actual entities.

Is this a bug, not supported or am I doing something incorrect?

class EquipmentType(Model):
    Name = F.TextField("Name")

class Category(Model):
    Name = F.TextField("Name")
    equipment_types = F.LinkField[EquipmentType]("equipment_types", EquipmentType)

class Template(Model):
    equipment_types = F.LookupField[EquipmentType]("equipment_types (from template_category)")
    template_category = F.LinkField[str]("template_category", Category)

This doesn't appear to be a bug per se but I can see how the documentation doesn't make it clear what to do here. LookupField will always return the list of values it gets back from the API. Try this instead; if that also doesn't work, then we've probably got a bug:

class Template(Model):
    template_category = F.LinkField[Category]("template_category", Category)
    equipment_types = F.LinkField[EquipmentType](
        field="equipment_types (from template_category)",
        model=EquipmentType,
        readonly=True
    )

Thanks @mesozoic - that works!