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

Annotate SlugRelatedField when field is a UUID #854

Closed
kaimcpheeters opened this issue Nov 13, 2022 · 4 comments · Fixed by #893
Closed

Annotate SlugRelatedField when field is a UUID #854

kaimcpheeters opened this issue Nov 13, 2022 · 4 comments · Fixed by #893
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@kaimcpheeters
Copy link

This is related to Annotate SlugRelatedField but specific to when the field is a UUID.

With the below model example, is it possible to annotate that the SlugRelatedField is a UUID rather than a just a string?

class TagSerializer(serializers.ModelSerializer):
    """Serializer for Tags"""
    workspace = serializers.SlugRelatedField(slug_field='uuid',  queryset=Workspace.objects.all(),)
    tag = serializers.UUIDField(source='uuid', read_only=True)

    class Meta (object):
        model = Tag
        fields = ('workspace', 'tag', 'name')

In the workspace view the schema specifies the workspace field to be a UUID
workspace | string <uuid>

In the tag view the schema specifies the workspace field to only be a string (through the SlugRelatedField)
workspace | string
tag | string <uuid>

@tfranzel
Copy link
Owner

Hi, as I tried to convey in the other issue, the SlugRelatedField field is by definition a plain string. At least that is my understanding of what DRF is intending. Departing from that may require some decorator trickery.

You can override the type of that field by using extend_schema_field. This is usually meant to be used as a decorator but also works in those rarer cases. You can then provide (among other things) a custom raw schema that fits your needs.

class TagSerializer(serializers.ModelSerializer):
    """Serializer for Tags"""
    workspace = extend_schema_field(field={'oneOf': [{'type': 'string'}, {'type': 'string', 'format': 'uuid'}]})(
        serializers.SlugRelatedField(slug_field='uuid', queryset=Workspace.objects.all())
    )

    tag = serializers.UUIDField(source='uuid', read_only=True)

    class Meta (object):
        model = Tag
        fields = ('workspace', 'tag', 'name')

@StopMotionCuber
Copy link
Contributor

I'm having an API making extensive use of the SlugRelatedField. From the documentation I don't have the impression that a SlugRelatedField needs to always be a string. Instead, my understanding (and also my usage) is that we want to do something similar as with a PrimaryKeyRelatedField, but we don't want to use the PrimaryKey, but an arbitary field.

To obtain the correct type for a field, we can handle it similarly to a PrimaryKeyRelatedField, we would just need to append here a source.append(field.slug_field).

@tfranzel
Copy link
Owner

tfranzel commented Dec 8, 2022

Looking at the doc, I may have been to quick to dismiss. The word slug usually means a string in the context of django, but given this, I think it is reasonable to look further.

It seems that SlugRelatedField is the less strict cousin of PrimaryKeyRelatedField and that any unique field in the related model is a candidate for it.

I will have a look at this again.

@tfranzel tfranzel added the enhancement New feature or request label Dec 8, 2022
tfranzel added a commit that referenced this issue Dec 8, 2022
thx to @StopMotionCuber for the `source.append(field.slug_field)` hint
@tfranzel
Copy link
Owner

tfranzel commented Dec 8, 2022

I think this is actually quite neat, given that SlugRelatedField is simply a non-pk version of PrimaryKeyRelatedField after all.

@kaimcpheeters @StopMotionCuber I would appreciate if you could test PR #893

@tfranzel tfranzel added the fix confirmation pending issue has been fixed and confirmation from issue reporter is pending label Dec 8, 2022
tfranzel added a commit that referenced this issue Dec 9, 2022
Treat SlugRelatedField analog to PrimaryKeyRelatedField #854
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants