我写了没有模型的Django API。我使用序列化器。在视图中,此api需要一个csv,当我调用get api时,它会给我我不需要的数据。我想要更有条理。但是我不知道显示格式化的json需要使用哪个功能。
view.py
private evaluate(value: number, rule: string) {
const matches: any = rule.match(/(\D+?)\s*(\d+)/);
if (matches === null) {
return null;
} else {
const operator = matches[1];
const number = matches[2];
this.logger.debug(`Value: ${value}, Operator: ${operator}, Number: ${number}`);
switch (operator) {
case '>' : return value > Number(number);
case '<' : return value < Number(number);
case '==' : return value === Number(number);
}
return null;
}
}
serializers.py
class bonolothaView(views.APIView):
def get(self, request):
# Load CSV Data
data = pd.read_csv("Data/ShapeUpData.csv", index_col=0)
# Load Specific Column in Dataframe
df = pd.DataFrame(data, columns = [ 'Challan Date' , 'Region', 'Net Sales', 'Qty'])
df.columns = df.columns.str.strip().str.replace(' ', '')
yourdata= [{"challandate": df["ChallanDate"], "region":df["Region"] , "qty": df["Qty"], "netsales": df["NetSales"]}]
print(yourdata)
results = bonolothaSerializer(yourdata, many=True).data
return Response(results)
data.csv标题为:
from rest_framework import serializers
class bonolothaSerializer(serializers.Serializer):
challandate = serializers.CharField()
region = serializers.CharField()
qty = serializers.CharField()
netsales = serializers.CharField()
challan Date Region Qty Sales
07/03/2017 Banani 1 7748
07/03/2017 Gulsan 1 7748
07/08/2017 Noakhali 2 8979
when i call the postman it gives me output like that:
我希望从我的csv中获得该类型的JSON文件。但是实际输出就是这样
[
{
"Challan Date":"07\/03\/2017",
"Region":"Dhaka North",
"Qty":1,
"Net Sales":7748
},
{
"Challan Date":"07\/03\/2017",
"Region":"Dhaka North",
"Qty":1,
"Net Sales":7748
}
]