我很困惑...
当视图尝试删除实例时,出现“ TypeError:'int'对象不可调用”的情况。
在“ form.instance.delete()”行上引发了异常
代码如下:
view.py
def decorate_letter(request, ...):
....
if request.POST:
flair_formset = flair_formset_factory(request.POST or None)
for form in flair_formset.forms:
if form.has_changed():
if form.is_valid():
if form.cleaned_data['delete'] == 1:
form.instance.delete()
elif form not in flair_formset.deleted_forms:
form.save()
models.py
class LetterFlair(models.Model):
letter = models.ForeignKey(Letter, on_delete=models.CASCADE)
flair = models.ForeignKey(Flair, on_delete=models.CASCADE, default=1)
x = models.DecimalField(decimal_places=2, max_digits=5, null=True, default=0)
y = models.DecimalField(decimal_places=2, max_digits=5, null=True, default=0)
r = models.DecimalField(decimal_places=2, max_digits=5, null=True, default=0)
placed = models.CharField(default='false', max_length=250)
delete = models.IntegerField(default=0)
forms.py
class LetterFlairModelForm(forms.ModelForm):
class Meta:
model = LetterFlair
fields = ['letter', 'flair', 'x', 'y', 'r', 'placed', 'delete']
堆栈跟踪
Traceback (most recent call last):
File ".../python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File ".../python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File ".../python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File ".../python3.6/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File ".../letters/views.py", line 140, in decorate_letter
form.instance.delete()
TypeError: 'int' object is not callable
什么可能导致异常?
更新
我已在问题中添加了堆栈跟踪。
我真的不确定我的代码的哪一部分导致了这个问题,因此发布的内容比平时更多。
更新2
现在,很明显错误是由何处产生的,出于我的考虑,我已精简了所发布的代码。
答案 0 :(得分:1)
@AgostBiro向我指出了正确的方向,并且查明了问题的原因,因为我将其中一个字段“ delete”命名为覆盖了delete()方法的“ delete”。重命名为已删除并迁移模型可解决此问题。