我正在写一个代表某些系统的应用程序:某些设备将包含硬件文章,例如,服务器将包含硬盘驱动器。
对于CRUD操作,我使用的是DRF ModelViewSet(没有覆盖任何内容)。
当我删除未设置m2m关系的设备时,它可以正常工作,但是当我将其设置为错误时。
这是模型方案的简化版本。
class Hardware(models.Model):
equipment = models.ManyToManyField('Equipment', blank=True, through='HardwareEQ')
# attributes
class HardwareEQ(models.Model):
hardware = models.ForeignKey(Hardware, on_delete=models.CASCADE)
equipment = models.ForeignKey(Equipment, on_delete=models.CASCADE)
# relation attributes
下面是完整的追溯。
Traceback:
File "C:\Users\USER\Envs\venv\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\USER\Envs\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\USER\Envs\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\USER\Envs\venv\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "C:\Users\USER\Envs\venv\lib\site-packages\rest_framework\viewsets.py" in view
103. return self.dispatch(request, *args, **kwargs)
File "C:\Users\USER\Envs\venv\lib\site-packages\rest_framework\views.py" in dispatch
483. response = self.handle_exception(exc)
File "C:\Users\USER\Envs\venv\lib\site-packages\rest_framework\views.py" in handle_exception
443. self.raise_uncaught_exception(exc)
File "C:\Users\USER\Envs\venv\lib\site-packages\rest_framework\views.py" in dispatch
480. response = handler(request, *args, **kwargs)
File "C:\Users\USER\Envs\venv\lib\site-packages\rest_framework\mixins.py" in destroy
93. self.perform_destroy(instance)
File "C:\Users\USER\Envs\venv\lib\site-packages\rest_framework\mixins.py" in perform_destroy
97. instance.delete()
File "C:\Users\USER\Envs\venv\lib\site-packages\django\db\models\base.py" in delete
918. collector.collect([self], keep_parents=keep_parents)
File "C:\Users\USER\Envs\venv\lib\site-packages\django\db\models\deletion.py" in collect
224. field.remote_field.on_delete(self, field, sub_objs, self.using)
File "C:\Users\USER\Envs\venv\lib\site-packages\django\db\models\deletion.py" in CASCADE
16. source_attr=field.name, nullable=field.null)
File "C:\Users\USER\Envs\venv\lib\site-packages\django\db\models\deletion.py" in collect
220. sub_objs = self.related_objects(related, batch)
File "C:\Users\USER\Envs\venv\lib\site-packages\django\db\models\deletion.py" in related_objects
236. **{"%s__in" % related.field.name: objs}
File "C:\Users\USER\Envs\venv\lib\site-packages\django\db\models\query.py" in filter
892. return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\USER\Envs\venv\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
910. clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\USER\Envs\venv\lib\site-packages\django\db\models\sql\query.py" in add_q
1290. clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\USER\Envs\venv\lib\site-packages\django\db\models\sql\query.py" in _add_q
1318. split_subq=split_subq, simple_col=simple_col,
File "C:\Users\USER\Envs\venv\lib\site-packages\django\db\models\sql\query.py" in build_filter
1202. value = self.resolve_lookup_value(value, can_reuse, allow_joins, simple_col)
File "C:\Users\USER\Envs\venv\lib\site-packages\django\db\models\sql\query.py" in resolve_lookup_value
1037. sub_value.resolve_expression(self, reuse=can_reuse, allow_joins=allow_joins)
Exception Type: TypeError at /Sycoma2/ensemble/2/
Exception Value: 'NoneType' object is not callable
要执行删除,我使用ajax请求调用hardareEN视图。 该视图是一个ModelViewSet,如下所示:
class HardwareENViewSet(viewsets.ModelViewSet):
queryset = HardwareEN.objects.all()
serializer_class = HardwareENSerializer
序列化器如下所示:
class HardwareENSerializer(VerboseSerializer):
class Meta:
model = HardwareEN
fields = '__all__'
def to_representation(self, instance):
"""
Add hardware data
:param instance:
:return:
"""
data = super().to_representation(instance)
for key, value in HardwareSerializer(instance.hardware).data.items():
try:
if not data[key]:
data[key] = value
except KeyError:
data[key] = value
return data
有关信息,我尝试删除to_representation
覆盖,但没有成功。
答案 0 :(得分:3)
问题是我覆盖了HardwareEN模型的__getattr__
方法,但是忘记为不存在的属性添加AttributeError
。因此,在获取模型属性时,框架在需要时没有出现属性错误。