I need to get the current object in ModelSerializer.validate() when it's being updated(not created)
Currently, I'm using self._args[0]
class UserInfoSerializer(serializers.ModelSerializer):
class Meta:
model = UserInfo
fields = ('id', 'Username')
def validate(self, data):
current_user = self._args[0] # <--
It's something like self.get_object()
in ViewSet
I wonder if there is a better way to get what I want, any idea? thanks
You can use the .instance
attribute
Example:
class UserInfoSerializer(serializers.ModelSerializer):
class Meta:
model = UserInfo
fields = ('id', 'Username')
def validate(self, data):
# initialization
s = UserInfoSerializer(data={"foo": "bar"}, )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.