Extend ModelSerializer autogenerated fields
See original GitHub issueDescribe the bug
It is not possible to extend fields which were autogenerated by a ModelSerializer
.
Defining a custom serializerfield and extending it results in loosing the Info provided by the model field.
To Reproduce
Suppose the following model:
class Word(SolidBaseProfile):
name = models.CharField(max_length=200, verbose_name=_("Titel"))
graphic = models.TextField(
max_length=500,
null=True,
blank=True,
verbose_name=_("Graphie"),
)
Furthermore let’s say we introduce a new “type” of string called “mdstring” to let our forntend know, that it has to render the contents as markdown formatted text.
If you define the following field + serializer
@extend_schema_field({"type": "mdstring"})
class MDStringField(serializers.CharField):
pass
class WordSerializer(serializers.ModelSerializer):
graphic = MDStringField()
class Meta:
model = Word
fields = "__all__"
The resulting Schema becomes:
Word:
type: object
properties:
id:
type: integer
readOnly: true
graphic:
type: mdstring
name:
type: string
title: Titel
maxLength: 200
Expected behavior
Since i’m not sure if this is a pure drf-spectacular
Problem i’m just stating what i would like to achive.
The schema i would like to see is:
Word:
type: object
properties:
id:
type: integer
readOnly: true
graphic:
type: mdstring
nullable: true
title: Graphie
maxLength: 500
name:
type: string
title: Titel
maxLength: 200
Issue Analytics
- State:
- Created 10 months ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
DRF: Is there any way to keep ModelSerializer's auto- ...
I'm trying to have Django Rest Framework serializers with dynamic fields that depend on the context passed (request method, CRUD action etc.) ...
Read more >Serializer fields - Django REST framework
Serializer fields handle converting between primitive values and internal datatypes. They also deal with validating input values, ...
Read more >There should be a way to override serializer fields in the ...
I've a model where few fields are auto-generated. How do I hide those fields from the Swagger UI during POST request?
Read more >Workflow & schema customization - drf-spectacular
Decorate your view functions with the @extend_schema decorator. ... GenericViewSet): @extend_schema( parameters=[ QuerySerializer, # serializer fields are ...
Read more >Serializing Django objects
If you only want a subset of fields to be serialized, you can specify a fields argument to the serializer: from django.core import...
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 Free
Top 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
Ooooooh that’s how the FieldExtensions ar supposed to work! Well that explains a lot now. And if I think about it: It’s somewhat similar to my suggestion above.
Thank you very much for your time and effort.
Consider this issue closed.
awesome!