-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
How to generate an enum of valid resourcetype values for use in schema with django-polymorphic? #958
Comments
I think this is related to the existing issue #885. I've found that adding - type = CharField(default="Album", required=False) ..to each serializer with the expected values does not work as it tries to serialize the "type" field to the model. |
It appears in the def create(self, validated_data: dict) -> SongMedium:
validated_data.pop("type", None) # do not persist type to model
return super().create(validated_data) |
I have reevaluated this issue. The blocker was that we couldn't just add the field to the sub-serializers because it would have been wrong if they were used individually outside of This is still true, but it looks like we now have more wiggle room due to upstream improvements. We can now create 2 components without too much trouble.
We will now generate an auxiliary component with an attached type field. The original components stay untouched and just get combined. The @lvlgl: Generating an extra |
This is working great! 🚀 Tested with My only suggestion is to insert the discriminator field at the top of the serializer rather than the bottom. Unless you know a way to re-order the serializer fields after the fact? |
awesome @axieum! thx for testing. How is inserting at the top any different than at the bottom? We gotta do it somewhere, and the next person will "prefer" it the other way round. Can you make an argument for this except preference? Reordering is not possible afaik, which would be part of the trade-off for this construction. |
Adding the type parameter first makes it more readable in my opinion. As changing the type causes the following (underneath) fields to change. It's a bit awkward having the type of resource drop-down show at the bottom of the payload in Redoc/Swagger, meaning you have to scroll to the bottom change the type and then scroll back up to see what fields changed. Does this make sense? I'm happy to provide a screenshot soon. |
Amended it. It looks slightly better in redoc but should not make much of a difference in general. It's a small concession we can make. |
Using this structure, I would be able to receive and update
SongSerializer.medium.resourcetype
. I was not able to generate schema for theresourcetype
field though. How could I generate an enumeration of validresourcetype
values for use in the schema?The text was updated successfully, but these errors were encountered: