How do I add a field to a django-rest-framework serializer that isn't on my model? -


i have serializer works fine get, post, delete actions. exposes model fields want. put action, user send values aren't built models , server deal how perform update on model. can send data using postman or curl , works browseable api still looks this: enter image description here

for put method want "is_winner", "num_hands_won", , "score" show instead of actual model fields. how do this? (let me know in comments if need more info)

statisticsserializer:

class statisticsserializer(serializers.modelserializer):     # pk = serializers.integerfield(required=false)     class meta:         model = statistics         fields = [             'url',             'games_won',             'hands_won',             'games_played',             'high_score',             'low_score',         ] 

statistics model:

class statistics(models.model):     # define model fields:     user = models.onetoonefield(user, primary_key=true)     games_won = models.integerfield(null=true, blank=true)     hands_won = models.integerfield(null=true, blank=true)     games_played = models.integerfield(null=true, blank=true)     high_score = models.integerfield(null=true, blank=true)     low_score = models.integerfield(null=true, blank=true)      def __str__(self):         return str(self.pk)      def increment_games_won(self, is_winner):         if is_winner true:             self.games_won = self.games_won + 1         return self.games_won      def add_to_hands_won(self, num_hands_won):         if num_hands_won > 0 , num_hands_won < 8:             self.hands_won = self.hands_won + num_hands_won         return self.hands_won      def increment_games_played(self):         self.games_played = self.games_played + 1         return self.games_played      def new_high_score(self, score):         if score > self.high_score:             self.high_score = score         return self.high_score      def new_low_score(self, score):         if score < self.low_score:             self.low_score = score         return self.low_score 

statistics viewset:

class statisticsviewset(defaultsmixin, viewsets.modelviewset):     queryset = statistics.objects.all()     serializer_class = statisticsserializer     filter_class = statisticsfilter     search_fields = ('pk', 'user')     ordering_fields = ('games_won', 'hands_won', 'games_played', 'high_score', 'low_score')      def update(self, request, pk=none):         stats = self.get_object()          stats.increment_games_won(request.data['is_winner'])         stats.add_to_hands_won(request.data['num_hands_won'])         stats.increment_games_played()         stats.new_low_score(request.data['score'])         stats.new_high_score(request.data['score'])         stats.save()          serialized_stats = statisticsserializer(stats, context={'request': request}).data         return response(serialized_stats) 

you use serializer , use put api
statisticsupdateserializer:

class statisticsupdateserializer:     is_winner = ...     num_hands_won = ...     score = ... 

and use serializer in put api or create new route shown in example mentioned in drf documentation here

@detail_route(methods=['post']) def set_password(self, request, pk=none):     user = self.get_object()      // use serializer below     serializer = passwordserializer(data=request.data)      if serializer.is_valid():         user.set_password(serializer.data['password'])         user.save()         return response({'status': 'password set'})     else:         return response(serializer.errors,                         status=status.http_400_bad_request) 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -