-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Description
Hello.
First, I've read the contribution guide over and over and I read this:
Feature requests will often be closed with a recommendation that they be implemented outside of the core REST framework library. Keeping new feature requests implemented as third party libraries allows us to keep down the maintenance overhead of REST framework, so that the focus can be on continued stability, bugfixes, and great documentation.
over and over before submitting this, but bear in mind. I'll explain with a usage example.
Second: This is more of a discussion to see your thoughts, I currently don't have the solution for this specific thing, I will just show my solutions for the sake of showing the problem, rather than solving it.
I have an Issue
related to Task
, so every task has some issues and an issue is related to one task, it's modeled in django and everything is fine regarding this. However, let's model it in DRF.
class TaskSerializer(serializers.ModelSerializer):
"""
There're lots of this x = XSerializer() inside TaskSerializer but I removed for simplicity,
it has more than 10 fields that need to be serialized.
"""
class Meta:
model = Task
exclude = []
class IssueSerializer(serializers.ModelSerializer):
task = TaskSerializer()
assigned = UserSerializer()
class Meta:
model = Issue
exclude = []
Let's think about something, many people opened issues and asked on stackoverflow about different serializers for "Input" and "Output" rather than "different request methods" or "actions", right?
I'm trying to create a new issue, and the issue has a task field, I want to mention the task with it's ID when I'm talking to the API, BUT I want the API to serialize the task when it's talking to me. I did the logic as in the screenshot, but I want to see if this is a behavior that we can have directly in DRF because it's the most reasonable thing to do instead of responding with PKs and requesting the data with that pk, ..etc, saving lots of trips between server and client(s).
All of this happened in a POST
request and the action is still create
. This is only typed as:
class IssueSerializer(serializers.ModelSerializer):
task = PrimaryKeyRelatedField(repr_serializer=TaskSerializer, queryset=Task.objects.all())
assigned = serializers.PrimaryKeyRelatedField(queryset=User.objects.all(), write_only=True)
class Meta:
model = Issue
exclude = []
and I added a field repr_serializer
to serialize the PKOnlyObject
to the object of type queryset.model
included in the initializer of the Field.
What do you think?