```python
try:
    thumbnail = (
        models.Thumbnail.objects
        .select_related('dimensions')
        .get(storage_hash=storage_hash, name=file.name))
except models.Thumbnail.DoesNotExist:
    thumbnail = None
else:
    try:
        dimensions = thumbnail.dimensions.width, thumbnail.dimensions.height
    except models.ThumbnailDimensions.DoesNotExist:
#...
if settings.THUMBNAIL_CACHE_DIMENSIONS and thumbnail and not dimensions_cache:

```