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.

Extend ModelSerializer autogenerated fields

See original GitHub issue

Describe 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:closed
  • Created 10 months ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
chgadcommented, Nov 29, 2022

I tested this and it works. You just have to set it up once I use this kind of thing myself for yaml string and even added validation to the fields.

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.

0reactions
tfranzelcommented, Nov 29, 2022

awesome!

Read more comments on GitHub >

github_iconTop 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 >

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