Are Serializer and BaseSerializer supported?
See original GitHub issueHi,
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:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have revisited this issue and I was too quick to reject the use case. It is in fact possible to use
ReadOnlyField
outside ofModelSerializer
, but I would call that an uncommon usage pattern. Most of the time I would assumeCharField(read_only=True)
is more appropriate asReadOnlyField
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 😄
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:But I still advocate for at least a better error message since that doesn’t seem like a serializer’s misusage to me 😄