Django评论ValueError

时间:2015-12-18 01:10:59

标签: python django django-rest-framework django-comments

我在发送到django-comments提供的端点时收到错误ValueError,如下所示我想解决: enter image description here

详细消息是“尝试获取内容类型'7'和对象PK'1'存在引发的ValueError”。我不知道为什么会出现这个ValueError,因为在admin中我可以发布评论,没有错误:

enter image description here

请注意,“我的用户”是管理部分内容类型列表中的第7个选项。

enter image description here

在comments.py中处理端点/ comments / post /的函数:

@csrf_protect
@require_POST
def post_comment(request, next=None, using=None):
    """
    Post a comment.

    HTTP POST is required. If ``POST['submit'] == "preview"`` or if there are
    errors a preview template, ``comments/preview.html``, will be rendered.
    """
    # Fill out some initial data fields from an authenticated user, if present
    data = request.POST.copy()
    if request.user.is_authenticated():
        if not data.get('name', ''):
            data["name"] = request.user.get_full_name() or request.user.get_username()
        if not data.get('email', ''):
            data["email"] = request.user.email

    # Look up the object we're trying to comment about
    ctype = data.get("content_type")
    object_pk = data.get("object_pk")
    if ctype is None or object_pk is None:
        return CommentPostBadRequest("Missing content_type or object_pk field.")
    try:
        model = apps.get_model(*ctype.split(".", 1))
        target = model._default_manager.using(using).get(pk=object_pk)
    except TypeError:
        return CommentPostBadRequest(
            "Invalid content_type value: %r" % escape(ctype))
    except AttributeError:
        return CommentPostBadRequest(
            "The given content-type %r does not resolve to a valid model." % escape(ctype))
    except ObjectDoesNotExist:
        return CommentPostBadRequest(
            "No object matching content-type %r and object PK %r exists." % (
                escape(ctype), escape(object_pk)))
    except (ValueError, ValidationError) as e:
        return CommentPostBadRequest(
            "Attempting go get content-type %r and object PK %r exists raised %s" % (
                escape(ctype), escape(object_pk), e.__class__.__name__))

我在自定义评论模型中定义的2个额外字段:

class ProfileComment(Comment):
    comment_OP_uri = models.CharField(max_length=300)
    comment_receiver = models.CharField(max_length=300)

the documentation后创建的相关表单:

class ProfileCommentForm(CommentForm):
    comment_OP_uri = forms.CharField(max_length=300)
    comment_receiver = forms.CharField(max_length=300)

    def get_comment_model(self):
        # Use our custom comment model instead of the default one.
        return ProfileComment

    def get_comment_create_data(self):
        # Use the data of the superclass, and add in the custom field
        data = super(ProfileComment, self).get_comment_create_data()
        data['comment_OP_uri', 'comment_receiver'] = self.cleaned_data['comment_OP_uri', 'comment_receiver']
        return data

1 个答案:

答案 0 :(得分:0)

通过将content_type值的值更改为users.MyUser

来解决此问题

apps.get_model需要app_name和model_name