2017-04-06 47 views
0

我有一個序列化程序,適用於GET,POST,DELETE操作。它公開了我想要的模型字段。但是,對於PUT操作,用戶將發回未構建到我的模型中的值,並且服務器將處理如何在模型上執行更新。我可以使用Postman或Curl發回數據,但它的工作原理是,但可瀏覽的API仍然如下所示:enter image description here如何將字段添加到不在我的模型中的django-rest-framework序列化程序?

對於PUT方法,我希望「is_winner」,「num_hands_won」和「score」實際的模型領域。我該怎麼做呢? (讓我知道在評論,如果你需要更多的信息)

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', 
     ] 

統計型號:

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 is 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 and 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 

統計視圖集:

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) 
+0

爲什麼你需要它在可瀏覽的API上? – creimers

+0

我不知道。它使得API在開發過程中更容易使用。 – FlashBanistan

+0

我明白了。我看到它的方式,可瀏覽的API基於自動生成(模型)表單。您可以深入瞭解其他框架源代碼,並瞭解如何自定義它,但這可能不值得付出努力......對於開發,我會推薦測試。 – creimers

回答

0

你也許可以使用其他串行,並用它爲你PUT API
StatisticsUpdateSerializer:

class StatisticsUpdateSerializer: 
    is_winner = ... 
    num_hands_won = ... 
    score = ... 

及認沽API中使用串行或創建如圖中提到的例子一條新路線DRF文檔here

@detail_route(methods=['post']) 
def set_password(self, request, pk=None): 
    user = self.get_object() 

    // Use your 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) 
+0

根據我的理解,不能將字段添加到不在模型中的序列化程序。 – FlashBanistan

相關問題