2010-10-19 24 views
1

我试图批量删除Django网站的一个开发实例上的所有注释,并且Django引发了一个AttributeException异常。AttributeError尝试批量删除Django中的项目时引发异常

我已经有了一个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' 

我不知道,为什么delete语句不工作。任何人都可以帮助我,为什么这不工作,我能做些什么来解决它?

关于我的模型的更多细节:

我有一个名为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) 

评论

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) 

投票

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薄荷9(Linux的2.6.32-21-通用)
Django的:1.2
数据库驱动程序:postgresql_psycopg2(2.2.1)

+1

是否有其他模型依赖于'Comment'模型?什么ForeignKey属性引用评论?我猜你的数据库有更多的“评论”模型。 – 2010-10-19 02:14:08

+0

@ S.Lott这是真的,我有一个模型,从'Comment'模型继承来添加一些额外的字段,我使用'django-voting'的'Vote'表,它有几个条目指向到继承模型。在这种情况下,Django不会执行“CASCADE DELETE”吗? – 2010-10-19 02:41:49

+0

@jeff charles:“在这种情况下,Django不会执行CASCADE DELETE吗?”为什么呢?你从哪里阅读过文档?是什么给了你这个想法呢? – 2010-10-19 02:43:21

回答

1

编辑:本来我以为你不能在一个QuerySet上执行delete()并且要推荐修补你迭代的项目,但显然你可以做这样的批量删除。尽管遍历QuerySet可能会给你一个更好的线索,看看有什么不对。

+0

忘记了这个:http://docs.djangoproject.com/en/1.2/ref/models/instances/#deleting-objects – 2010-10-19 02:05:41

+0

对QuerySet进行迭代不会引发任何异常,并且可以像我期望的那样工作 – 2010-10-19 02:37:49

+0

对不起,迭代没有删除似乎按预期工作(将尝试w /删除明天早上) – 2010-10-19 02:44:37

相关问题