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'