Created
November 4, 2021 16:07
-
-
Save kyungjunleeme/68b772324aaf932f8bde6fd7f97633de to your computer and use it in GitHub Desktop.
create current user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MeetingCreateSerializer(serializers.ModelSerializer): | |
tracks = serializers.PrimaryKeyRelatedField(many=True, read_only=True) | |
def create(self, validated_data): | |
"""1. Override ``create`` to provide a user via request.user by default. | |
This is required since the read_only ``user`` field is not included by | |
default anymore since | |
https://github.com/encode/django-rest-framework/issues/6031 | |
https://github.com/encode/django-rest-framework/pull/5886. | |
2.read_only | |
Read-only fields are included in the API output, but should not be included in the input during create or update operations. Any 'read_only' fields that are incorrectly included in the serializer input will be ignored. | |
Set this to True to ensure that the field is used when serializing a representation, but is not used when creating or updating an instance during deserialization. | |
Defaults to False | |
""" | |
if "user" not in validated_data: | |
validated_data["user"] = self.context["request"].user | |
meeting = Meeting.objects.create(**validated_data) | |
return meeting | |
class Meta: | |
model = Meeting | |
fields = [ | |
"title", | |
"meeting_date", | |
"meeting_starttime", | |
"meeting_end_time", | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment