django/django · error · IndexError

Invalid OFT field name given: %s.

Error message

Invalid OFT field name given: %s.

What it means

Raised by Feature.index (django/contrib/gis/gdal/feature.py:119) as an IndexError when the OGR C function OGR_F_GetFieldIndex returns a value < 0, meaning no field with the given name exists on this feature's layer definition. It is triggered indirectly by `feat['bad_name']` and `feat.get('bad_name')`.

Source

Thrown at django/contrib/gis/gdal/feature.py:119

    def geom_type(self):
        "Return the OGR Geometry Type for this Feature."
        return OGRGeomType(capi.get_fd_geom_type(self._layer._ldefn))

    # #### Feature Methods ####
    def get(self, field):
        """
        Return the value of the field, instead of an instance of the Field
        object. May take a string of the field name or a Field object as
        parameters.
        """
        field_name = getattr(field, "name", field)
        return self[field_name].value

    def index(self, field_name):
        "Return the index of the given field name."
        i = capi.get_field_index(self.ptr, force_bytes(field_name))
        if i < 0:
            raise IndexError("Invalid OFT field name given: %s." % field_name)
        return i

View on GitHub (pinned to ae25a40be0)

Solutions

  1. List available fields first: `print(feat.fields)`.
  2. Match case exactly, or normalize: `name = next((f for f in feat.fields if f.lower() == wanted.lower()), None)`.
  3. Guard with membership check before access.
  4. Confirm the field exists at the layer definition level with `ogrinfo`.

Example fix

// before
val = feat['Discription']   # typo -> IndexError
// after
name = next(f for f in feat.fields if f.lower() == 'description')
val = feat[name].value
Defensive patterns

Strategy: validation

Validate before calling

wanted = wanted.strip()
match = next((f for f in feat.fields if f.lower() == wanted.lower()), None)
if match is None:
    raise KeyError(f'no field matching {wanted!r}; have {feat.fields}')
val = feat[match].value

Type guard

def field_exists(feat, name) -> bool:
    return name in feat.fields

Try / catch

try:
    val = feat[name].value
except IndexError:
    val = None  # field absent; handle gracefully

Prevention

When it happens

Trigger: `feat['TypoName']`. Case mismatch (`feat['description']` vs. actual `'Description'`). Reading a field that exists in a different layer. Schema changed and the field was renamed or removed.

Common situations: Source data produced by a different tool with different column naming/casing. Locale or encoding differences in field names. Stale code referencing renamed columns. Whitespace in field names not stripped.

Related errors


AI-assisted analysis of django/django@ae25a40be0 (2026-08-06). Data as JSON: /api/errors/230620ce2f6d2407. Report an issue: GitHub.