检查设备是否已存在于组中

时间:2017-03-06 01:07:27

标签: python django django-rest-framework

如何使用列表表达式检查设备在发布时是否已存在?用户可能将同一设备发布在同一组中。我已完成发布部分,但不知道检查组中设备是否存在的有效方法。

这是我的帖子功能

def post(self, request, token=None, format=None):
    group_instance = Group.objects.get(token=token)
    for device_token in request.data['devices']:
        # device = Device.objects.get(token=device_token, owner=request.user)
        device = Device.objects.select_related('group').get(token=device_token, owner=request.user)
        # will not hit the db as value is already fetched in the previous query
        device.group = group_instance
        # check if device exist in a group or not
        print('device', device)
        device.save()
    return Response(device, status.HTTP_200_OK)

0 个答案:

没有答案