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

Introduce a serializer field for GenericForeignKeys #17936

Open
jeremystretch opened this issue Nov 5, 2024 · 0 comments
Open

Introduce a serializer field for GenericForeignKeys #17936

jeremystretch opened this issue Nov 5, 2024 · 0 comments
Labels
status: backlog Awaiting selection for work type: housekeeping Changes to the application which do not directly impact the end user

Comments

@jeremystretch
Copy link
Member

Proposed Changes

Devise and implement a custom serializer field to represent GenericForeignKeys on models. This is currently handled using a read-only SerializerMethodField, which requires a boilerplate method be included on the serializer class. For instance, the code below is from the serializer for circuit terminations:

    @extend_schema_field(serializers.JSONField(allow_null=True))
    def get_termination(self, obj):
        if obj.termination_id is None:
            return None
        serializer = get_serializer_for_model(obj.termination)
        context = {'request': self.context['request']}
        return serializer(obj.termination, nested=True, context=context).data

Justification

Introducing a custom field to represent GFK relationships allows us to enforce a standard implementation and reduce boilerplate.

@jeremystretch jeremystretch added type: housekeeping Changes to the application which do not directly impact the end user status: backlog Awaiting selection for work labels Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: backlog Awaiting selection for work type: housekeeping Changes to the application which do not directly impact the end user
Projects
None yet
Development

No branches or pull requests

1 participant