在Django中批量删除项目时引发AttributeError异常
我正在尝试在我的Django网站的开发环境中批量删除所有评论,但Django却抛出了一个AttributeException的错误。
我在Python命令行中运行了以下代码:
>>> from django.contrib.comments.models import Comment
>>> Comment.objects.all().delete()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/jeff/.virtualenvs/osl_main-website/lib/python2.6/site-packages/django/db/models/query.py", line 441, in delete
obj._collect_sub_objects(seen_objs)
File "/home/jeff/.virtualenvs/osl_main-website/lib/python2.6/site-packages/django/db/models/base.py", line 569, in _collect_sub_objects
sub_obj._collect_sub_objects(seen_objs, self, related.field.null)
File "/home/jeff/.virtualenvs/osl_main-website/lib/python2.6/site-packages/django/db/models/base.py", line 585, in _collect_sub_objects
delete_qs = rel_descriptor.delete_manager(self).all()
AttributeError: 'ReverseSingleRelatedObjectDescriptor' object has no attribute 'delete_manager'
我不太明白为什么删除的操作没有成功。有没有人能帮我看看这是为什么,以及我该如何解决这个问题?
关于我的模型的额外信息:
我有一个叫做OslComment
的模型,它是从Comment
模型继承而来的。我还有一个Vote
模型,它指向OslComment
中的条目。
BaseCommentAbstractModel
class BaseCommentAbstractModel(models.Model):
"""
An abstract base class that any custom comment models probably should
subclass.
"""
# Content-object field
content_type = models.ForeignKey(ContentType,
verbose_name=_('content type'),
related_name="content_type_set_for_%(class)s")
object_pk = models.TextField(_('object ID'))
content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
# Metadata about the comment
site = models.ForeignKey(Site)
Comment
class Comment(BaseCommentAbstractModel):
"""
A user comment about some object.
"""
# Who posted this comment? If ``user`` is set then it was an authenticated
# user; otherwise at least user_name should have been set and the comment
# was posted by a non-authenticated user.
user = models.ForeignKey(User, verbose_name=_('user'),
blank=True, null=True, related_name="%(class)s_comments")
user_name = models.CharField(_("user's name"), max_length=50, blank=True)
user_email = models.EmailField(_("user's email address"), blank=True)
user_url = models.URLField(_("user's URL"), blank=True)
comment = models.TextField(_('comment'), max_length=COMMENT_MAX_LENGTH)
# Metadata about the comment
submit_date = models.DateTimeField(_('date/time submitted'), default=None)
ip_address = models.IPAddressField(_('IP address'), blank=True, null=True)
is_public = models.BooleanField(_('is public'), default=True,
help_text=_('Uncheck this box to make the comment effectively ' \
'disappear from the site.'))
is_removed = models.BooleanField(_('is removed'), default=False,
help_text=_('Check this box if the comment is inappropriate. ' \
'A "This comment has been removed" message will ' \
'be displayed instead.'))
OslComment
class OslComment(Comment):
parent_comment = models.ForeignKey(Comment, blank=True, null=True, related_name='parent_comment')
inline_to_object = models.BooleanField(default=False)
edit_timestamp = models.DateTimeField()
transformed_comment = models.TextField(editable=False)
is_deleted_by_user = models.BooleanField(default=False)
Vote
class Vote(models.Model):
"""
A vote on an object by a User.
"""
user = models.ForeignKey(User)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
object = generic.GenericForeignKey('content_type', 'object_id')
vote = models.SmallIntegerField(choices=SCORES)
其他信息:
Python版本:2.6.5
操作系统:Linux Mint 9 (Linux 2.6.32-21-generic)
Django版本:1.2
数据库驱动:postgresql_psycopg2 (2.2.1)
1 个回答
1
编辑:最开始我以为你不能在QuerySet上使用delete(),所以打算建议你一个一个地处理这些项目,但显然你可以这样进行批量删除。不过,试着遍历一下QuerySet,可能会让你更清楚地知道哪里出了问题。