Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration with django-filter: 'Meta.fields' must not contain non-model field names #520

Open
mfoglio opened this issue May 17, 2022 · 1 comment

Comments

@mfoglio
Copy link

mfoglio commented May 17, 2022

Hello, I have the following models representing some Data that can be created by an Annotator which can either be a human UserAnnotator or an AI AiModelAnnotator

class Annotator(PolymorphicModel):
    pass


class UserAnnotator(Annotator):
    user = models.ForeignKey(
        get_user_model(),
        on_delete=models.PROTECT,
        default=None
    )

class AiModelAnnotator(Annotator):
    ai_model = models.CharField(max_length=255)
    version = models.CharField(max_length=16, default=None, null=True)

class Data(models.Model):
    annotator = models.ForeignKey(Annotator, on_delete=models.PROTECT)

I am using django-rest-framework and django-filter to expose an API endpoint where I can look for data produced by a specific AI model.
The serializers are the following:

class AiModelAnnotatorSerializer(serializers.ModelSerializer):
    class Meta:
        model = AiModelAnnotator
        fields = '__all__'


class UserAnnotatorSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserAnnotator
        fields = '__all__'


class AnnotatorSerializer(serializers.ModelSerializer):
    class Meta:
        model = Annotator
        fields = '__all__'


class AnnotatorPolymorphicSerializer(PolymorphicSerializer):
    model_serializer_mapping = {
        Annotator: AnnotatorSerializer,
        AiModelAnnotator: AiModelAnnotatorSerializer,
        UserAnnotator: UserAnnotatorSerializer,
    }

class AnnotationSerializer(serializers.ModelSerializer):
    annotator = serializers.PrimaryKeyRelatedField(queryset=Annotator.objects.all())
    class Meta:
        model = Annotation
        fields = '__all__'

And the viewset is:

class AnnotationTrainingViewSet(
        mixins.CreateModelMixin,
        mixins.ListModelMixin,
        mixins.RetrieveModelMixin,
        viewsets.GenericViewSet
    ):
    queryset = Annotation.objects.all()
    serializer_class = AnnotatorPolymorphicSerializer
    filterset_fields = ('annotator', 'annotator__ai_model')

When I try to call the GET endpoint with the error 'Meta.fields' must not contain non-model field names: annotator__ai_model which is caused by adding annotator__ai_model to the AnnotationTrainingViewSet. Without specifying annotator__ai_model the code works fine but of course I can't use django-filter as I'd like to.
Is this the expected behavior? Is django-filter supported?

@lvlgl
Copy link

lvlgl commented Apr 18, 2023

I am also searching for a solution to this issue. While I have been able to find workarounds for some cases, these workarounds are not able to accommodate different types of lookups. Therefore, I am hoping that @vdboor will provide an answer soon clarifying how we can properly solve this problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants