Add a new AsymmetricRelatedField to DRF #8966
-
Hey folks. I've been using DRF for a while and one of the neat things I picked up a few years ago is a class AsymmetricRelatedField(serializers.PrimaryKeyRelatedField):
def __init__(
self, serializer_class: Any, *args: Any, **kwargs: Any
) -> None:
self.serializer_class = serializer_class
super().__init__(*args, **kwargs)
def to_representation(self, value: Any) -> Any:
return self.serializer_class(value).data
class MySerializer:
destination = AsymmetricRelatedField(
CitySerializer, queryset=City.objects.all()
) This should be pretty self-explanatory. it simply lets you use the same field for accepting a Primary key and returns an object based on a serializer in the response. Would it be a good idea to have this within DRF or is this something that's too basic? If I were to implement this as core functionality, I would add the The If this sounds like a good idea, I'd love to open my first PR for it! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
At this point of this projects life cycle, it's highly unlikely to add new feature to DRF core unless it is supported in django. so I request to send a PR here https://github.com/chibisov/drf-extensions and ping me |
Beta Was this translation helpful? Give feedback.
At this point of this projects life cycle, it's highly unlikely to add new feature to DRF core unless it is supported in django. so I request to send a PR here https://github.com/chibisov/drf-extensions and ping me