def cf(device, field):
    """Get the value for the specified custom field name.

    This, combined with the a prefetch_related() results into much more
    efficient access of custom fields and their values. See:
    https://github.com/digitalocean/netbox/issues/3185

    Be warned that this treats empty values as non-existing fields.
    """
    for cfv in device.custom_field_values.all():
        if cfv.field.name == field:
            return cfv.value
    return None