GeeWee / django-auto-prefetching

Automatic prefetching for Django
MIT License
231 stars 18 forks source link

FieldError using HyperlinkedIdentityField #3

Closed Nekmo closed 5 years ago

Nekmo commented 5 years ago

Exception:

FieldError at /api/web_content/engine_configs/
Invalid field name(s) given in select_related: '*'. Choices are: (none)

select_related is "*" in prefetch: Screenshot_20190814_140612

Workaround: ignore HyperlinkedIdentityField fields:

    for name, field_instance in fields:
        field_type_name = field_instance.__class__.__name__
        logger.debug(
            f'{" " * indentation} Field "{name}", type: {field_type_name}, src: "{field_instance.source}"'
        )

        # We potentially need to recurse deeper
        if isinstance(field_instance, (BaseSerializer, RelatedField, ManyRelatedField)) \
                and not isinstance(field_instance, HyperlinkedIdentityField):
            # ...

Serializer:

class EngineUpdateSerializer(HispaSerializerMixin, serializers.ModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name='web_contents:engineupdate-detail')
    config = SimpleEngineConfigSerializer()
    first_update = SimpleEngineUpdateSerializer()
    parent = SimpleEngineUpdateSerializer()

    class Meta:
        model = EngineUpdate
GeeWee commented 5 years ago

Should be fixed in 0.1.7!