代码之家  ›  专栏  ›  技术社区  ›  Jasonca1

如何从DRF模型序列化程序中去除返回字段的空白?

  •  0
  • Jasonca1  · 技术社区  · 5 年前

    class ObjectSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.MyModel
        fields = ('field1', 'field2')
    

    我的查询代码如下所示:

    class MyAPI(ListAPIView):
    http_method_names = ['get']
    serializer_class = ObjectSerializer
    
    def get_queryset(self):
        queryset = MyModel.objects.values('field1', 'field2').filter(someField='SomeValue').all()
        return queryset
    

    extra_kwargs = {"content": {"trim_whitespace": True}}

    编辑:模型代码如下所示:

    class MyModel(models.Model):
    field1 = models.CharField(db_column='FIELD1', max_length=24)  # Field name made lowercase.
    field2 = models.CharField(db_column='FIELD2', max_length=60, blank=True, null=True)  # Field name made lowercase.
    
    class Meta:
        managed = False
        db_table = 'mytable'
    
    1 回复  |  直到 5 年前