尝试删除对象时Django Admin崩溃

时间:2014-02-28 12:20:13

标签: python django django-admin

我有一个问题,当我尝试删除一个对象时,Django Admin(我运行Django 1.3)会崩溃,而管理员试图显示哪些子对象会受到该操作的影响。我能够在终端中删除对象。这是追溯:

File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py", line 111, in get_response 
response = callback(request, *callback_args, **callback_kwargs) 
File "/usr/lib/python2.7/dist-packages/django/contrib/admin/options.py", line 307, in wrapper 
return self.admin_site.admin_view(view)(*args, **kwargs) 
File "/usr/lib/python2.7/dist-packages/django/utils/decorators.py", line 93, in _wrapped_view 
response = view_func(request, *args, **kwargs) 
File "/usr/lib/python2.7/dist-packages/django/views/decorators/cache.py", line 79, in _wrapped_view_func 
response = view_func(request, *args, **kwargs) 
File "/usr/lib/python2.7/dist-packages/django/contrib/admin/sites.py", line 197, in inner 
return view(request, *args, **kwargs) 
File "/usr/lib/python2.7/dist-packages/django/utils/decorators.py", line 28, in _wrapper 
return bound_func(*args, **kwargs) 
File "/usr/lib/python2.7/dist-packages/django/utils/decorators.py", line 93, in _wrapped_view 
response = view_func(request, *args, **kwargs) 
File "/usr/lib/python2.7/dist-packages/django/utils/decorators.py", line 24, in bound_func 
return func(self, *args2, **kwargs2) 
File "/usr/lib/python2.7/dist-packages/django/db/transaction.py", line 217, in inner 
res = func(*args, **kwargs) 
File "/usr/lib/python2.7/dist-packages/django/contrib/admin/options.py", line 1201, in delete_view 
[obj], opts, request.user, self.admin_site, using) 
File "/usr/lib/python2.7/dist-packages/django/contrib/admin/util.py", line 104, in get_deleted_objects 
to_delete = collector.nested(format_callback) 
File "/usr/lib/python2.7/dist-packages/django/contrib/admin/util.py", line 158, in nested 
roots.extend(self._nested(root, seen, format_callback)) 
File "/usr/lib/python2.7/dist-packages/django/contrib/admin/util.py", line 141, in _nested 
children.extend(self._nested(child, seen, format_callback)) 
File "/usr/lib/python2.7/dist-packages/django/contrib/admin/util.py", line 141, in _nested 
children.extend(self._nested(child, seen, format_callback)) 
File "/usr/lib/python2.7/dist-packages/django/contrib/admin/util.py", line 136, in _nested 
if obj in seen: 
File "/test.py", line 371, in __eq__ 
return len(self.text) == len(other.text) 

AttributeError: 'ChildB' object has no attribute 'text'

这就是我的模型:

class Parent(models.Model):  pass

class ChildA(models.Model):

    parent = models.ForeignKey(Parent)
    text = models.CharField()

    def __eq__(self, other):
        return len(self.text) == len(other.text)

class ChildB(models.Model):

    parent = models.ForeignKey(Parent)
    counter = models.IntegerField()

为什么Django Admin会尝试比较ChildA和ChildB?我已经修复了这个问题,在比较文本长度之前添加一个检查以确保它是正确的类型,但是想知道是否有其他人遇到过这个问题以及我的结构是否存在问题。更奇怪的是,并非所有父对象都会发生这种情况。

谢谢,马蒂亚斯

1 个答案:

答案 0 :(得分:0)

模型不正确

from django.db import models
class Parent(models.Model):
    pass

class ChildA(models.Model):

    parent = models.ForeignKey(Parent)
    text = models.CharField()

    def __eq__(self, other):
        return len(self.text) == len(other.text)

class ChildB(models.Model):

    parent = models.ForeignKey(Parent)
    counter = models.IntegerField()

eq 功能可能导致此错误,请将其标记并重试

    def __eq__(self, other):
        return len(self.text) == len(other.text)