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.

Are Serializer and BaseSerializer supported?

See original GitHub issue

Hi,

I am using DRF’s serializers.Serializer and serializers.BaseSerializer in a few places. I am running into errors because spectacular tries to access attributes on the serializer that are not present.

https://github.com/tfranzel/drf-spectacular/blob/deb83250f078ed4bce17b12972e098409ac69ec9/drf_spectacular/openapi.py#L763 throws AttributeError because my BaseSerializer subclass does not have fields.

https://github.com/tfranzel/drf-spectacular/blob/deb83250f078ed4bce17b12972e098409ac69ec9/drf_spectacular/openapi.py#L526 throws AttributeError because my Serializer subclass does not have Meta.

  • Is it intended that spectacular only supports ModelSerializer subclasses, or am I maybe missing something here?
  • Is there a good workaround for this?
  • As a last resort solution, is there an API / pattern that allows me to manually define the schema instead of relying on inspection? I checked https://drf-spectacular.readthedocs.io/en/latest/customization.html, but I am not sure what the right approach would be in this case. A custom preprocessing hook maybe?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tfranzelcommented, Aug 25, 2021

I have revisited this issue and I was too quick to reject the use case. It is in fact possible to use ReadOnlyField outside of ModelSerializer, but I would call that an uncommon usage pattern. Most of the time I would assume CharField(read_only=True) is more appropriate as ReadOnlyField is in fact made for accessing the models. Nonetheless if it works for DRF, we must handle the case.

@Chadys I added a check to prevent the exception and warn on the situation. Warning resolution via decoration however is not that easy here. I would suggest following the new warning advice 😄

0reactions
Chadyscommented, Aug 23, 2021

For anyone stumbling on the exact same issue as me, where I customize rest_framework_simplejwt’s serializers and that makes drf-spectacular crash on ReadOnlyField, the simpler and cleaner way to resolve this is:

class YourCustomTokenObtainPairSerializerExtension(TokenObtainPairSerializerExtension):
    target_class = "your_app_name.serializers.YourCustomTokenObtainPairSerializer"

class YourCustomTokenRefreshSerializerExtension(TokenRefreshSerializerExtension):
    target_class = "your_app_name.serializers.YourCustomTokenRefreshSerializer"

But I still advocate for at least a better error message since that doesn’t seem like a serializer’s misusage to me 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serializers - Django REST framework
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily ...
Read more >
BaseSerializer -- Classy DRF
The BaseSerializer class provides a minimal class which may be used for writing custom serializer implementations. Note that we strongly restrict the ...
Read more >
Serializers - Django REST Framework - GeeksforGeeks
Serializers in Django REST Framework are responsible for converting objects into data types understandable by javascript and front-end ...
Read more >
Serializers - Python Rest Framework
BaseSerializer class that can be used to easily support alternative serialization and deserialization styles. This class implements the same basic API as ...
Read more >
Serializers - Django REST framework - Tom Christie
Django, API, REST, Serializers, ModelSerializer, ... and is only required if we want our serializer to support deserialization into fully fledged object ......
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