使用Django 1.11,我有一个模型,该模型具有要通过get请求返回的特定属性(在我的情况下,get_queryset返回对get请求的响应),并且它仅返回给定模型实例的特定属性( aka对象,aka记录)。 我想要记录的所有属性,而不仅仅是django默认情况下决定使用查询集给我的那些属性。
我尝试过使用
ekg.objects.all().filter(user=request.user).values('user', 'id')
-试图使数据库返回的特定值无济于事。
型号:
class Ekg(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, blank = False, null=False)
ekgId = models.AutoField(primary_key=True)
sampling_rate = models.DecimalField(max_digits=5, decimal_places=2, null = False, blank = False)
dataTag2 = models.TextField(null = False, blank= False)
dataTag3 = models.TextField(null = False, blank= False)
timestamp = models.DateTimeField(auto_now_add=True, null=False, blank=False) #date and time of the signal added to db
观看次数:
class EkgAPIView(mixins.CreateModelMixin, generics.ListAPIView):
permission_classes= [permissions.IsAuthenticated]
authentication_classes= [JSONWebTokenAuthentication]
serializer_class = EkgSerializer
def get_queryset(self):
#return all
qs = Ekg.objects.all().filter(user=request.user).filter(ekgId=2).values('user', 'ekgId', 'sampling_rate', 'dataTag2', 'dataTag3', 'timestamp')
return qs
生成输出的脚本(忽略jwt身份验证,Response.text生成输出):
def do(method='get', data={}, is_json=True):
headers = {"Content-Type": "application/json",
"Authorization": "JWT " + token} #authorization header required with jwt per restconfig/main.py JWT_AUTH configuration}
if is_json:
headers['content-type'] = 'application/json'
data = json.dumps(data)
r = requests.request(method, EKG_ENDPOINT, data=data, headers=headers)
print(r.text)
print(r.status_code)#make sure its actually running
return r
data = {
}
do( data=data)
使用api端点输出(不需要):
[{
"sampling_rate":"212.00",
"dataTag2":"anotherDatatag2",
"dataTag3":"anotherDatatag3"
}]
预期(时间戳不完全是这种格式,但是会发回给我):
[{
"ekgId":"2",
"timestamp":"July 10, 2019 - 22:19:47",
"sampling_rate":"212.00",
"dataTag2":"anotherDatatag2",
"dataTag3":"anotherDatatag3"
}]