I'm trying to bulk delete all of the comments on a dev instance of my Django website and Django is raising an AttributeException.
I've got the following code on a python prompt:
>>> 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 'dele开发者_高级运维te_manager'
I'm not sure as to why the delete statement is not working. Can anyone help me with why this isn't working and what I can do to fix it?
Additional details about my models:
I have another model called OslComment
that inherits from Comment
. I also have a Vote
model that points to entries in 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)
Misc information:
Python Version: 2.6.5
Operating System: Linux Mint 9 (Linux 2.6.32-21-generic) Django: 1.2 Database driver: postgresql_psycopg2 (2.2.1)Edited: Originally I thought you couldn't do delete() on a QuerySet and was going to recommend you iterate over the items, but apparently you can do bulk deletes like that. Trying to iterate over the QuerySet might give you a better clue as to what's wrong, though.
精彩评论