question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hide serializer field in inline serializer

See original GitHub issue

Hey, I’m trying to use part of serializer as a field. I know of the existence of extend_schema_serializer and the exclude_fields options but I cannot manage to use it as a field of a different serializer.

Example:

responses=extend_schema_serializer(many=False)(
    inline_serializer(
        name="getHeartBeatsResponse",
        fields={
            "count": serializers.IntegerField(),
            "page_count": serializers.IntegerField(),
            "results": serializers.ListField(child=DeviceHeartbeatSerializer()),       # Here I want a exclude a field from DeviceHeartbeatSerializer
        },
    )
),

I tried

responses=extend_schema_serializer(many=False)(
    inline_serializer(
        name="getHeartBeatsResponse",
        fields={
            "count": serializers.IntegerField(),
            "page_count": serializers.IntegerField(),
            "results": serializers.ListField(
                child=extend_schema_serializer(exclude_fields="device")(
                    DeviceHeartbeatSerializer()
                )
            ),
        },
    )
),

but it does not seem to work. Does this feature simply not exist or I am I using the library in a wrong way?

Pardon me, if the answer is simply but I could not find it in the docs.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
t-pohlcommented, May 24, 2021

Okay, I can happily live with the workaround as long as I know about the limitation. Thank you very much for clarifying and helping me out!

0reactions
tfranzelcommented, May 24, 2021

this is the way.

i’m afraid this is a conceptual limitation. we would have 2 “different” serializer and only one name for it. unless we would attach ugly suffixes like DeviceHeartbeat2 this cannot be resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to hide field that is foreing key in serializer based on a ...
2 Answers 2 ; import Prefetch class SparkleTemplateView ; ListAPIView): serializer_class = SparkleTemplateSerializer def get_queryset ; self): user ...
Read more >
HiddenField in serializers - Django REST Framework
HiddenField is a field class that does not take a value based on user input, but instead takes its value from a default...
Read more >
Hiding Fields in Serializers · Issue #100 · tfranzel/drf-spectacular
I tried creating a custom serializer field (e.g. MyField ) and annotating it with @extend_schema_field(OpenApiTypes.NONE) , which does not cause ...
Read more >
Serializer relations - Django REST framework
When using the ModelSerializer class, serializer fields and relationships will be automatically generated for you. Inspecting these automatically generated ...
Read more >
Hide the (obvious) relation from nested serializer in DRF-django
It should be fairly obvious that the "document" field from ItemsSerializer should be derived from the parent serializer at the time of storage....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found