我正在开发一个应用程序,它的功能之一是可以识别该设备,但有时无法收回任何东西。我正在使用的API可能有一个错误。
设备型号如下:
class DeviceSerializer(ModelSerializer):
def create(self, validated_data):
# Check if exist
device = Device.objects.filter(user=validated_data["user"], deviceId=validated_data['deviceId']).first()
today = timezone.now()
# If exist, update
if device:
device.tokenId = validated_data['tokenId']
device.updated_at = today
device.save()
logger.info("[Device] Updated deviceID {} with token {} for user {}".format(
validated_data['deviceId'],
validated_data['tokenId'],
validated_data["user"].username
))
else:
device = Device(**validated_data)
device.save()
logger.info("[Device] Created deviceID {} with token {} for user {}".format(
validated_data['deviceId'],
validated_data['tokenId'],
validated_data["user"].username
))
# Associate Device to ActiveSession
active_session = ActiveSession.get_active_session(self.context.get('request'))
active_session.device = device
active_session.save()
return device
class Meta:
model = Device
fields = ("id", "deviceModel", "deviceId", "packageId", "tokenId", "platformId")
而序列化器是:
active_session.device = device
我收到的错误来自此行AttributeError
我想为此[ERROR] 15:12 Internal Server Error: /api/device/
Traceback (most recent call last):
File "/home/user/.local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/home/user/.local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/user/.local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/user/.local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/user/.local/lib/python2.7/site-packages/rest_framework/viewsets.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "/home/user/.local/lib/python2.7/site-packages/rest_framework/views.py", line 483, in dispatch
response = self.handle_exception(exc)
File "/home/user/.local/lib/python2.7/site-packages/rest_framework/views.py", line 443, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/user/.local/lib/python2.7/site-packages/rest_framework/views.py", line 480, in dispatch
response = handler(request, *args, **kwargs)
File "/home/user/Downloads/ID/id/modules/api/views.py", line 69, in create
self.perform_create(serializer)
File "/home/user/Downloads/ID/id/modules/api/views.py", line 78, in perform_create
serializer.save(user=self.request.user)
File "/home/user/.local/lib/python2.7/site-packages/rest_framework/serializers.py", line 214, in save
self.instance = self.create(validated_data)
File "/home/user/Downloads/ID/id/modules/api/serializers.py", line 65, in create
active_session.device_test = device
AttributeError: 'NoneType' object has no attribute 'device_test'
创建一个例外来面对这个错误。但是我的思想是开放的。
完整的回溯是这样的:
{{1}}
答案 0 :(得分:1)
在许多情况下都可能发生AttributeError,但在尝试与相关模型进行错误关联时经常发生。在这种情况下,如果您的ActionveSessionModel与'device'具有不同的related_name,则不会找到'device'。
https://github.com/django/django/blob/master/django/db/models/fields/related_descriptors.py#L98