Search code examples
pythondjangodjango-rest-frameworkdjango-comments

Django comments ValueError


I am getting an error ValueError when posting to an endpoint supplied by django-comments as follows which I would like to resolve: enter image description here

The message in detail is "Attempting go get content-type '7' and object PK '1' exists raised ValueError". I am not sure why this ValueError is raised as in the admin I am able to post comments as such with no errors:

enter image description here

Note that 'my user' is the 7th option in the content type list in the admin section.

enter image description here

Function that handles the endpoint /comments/post/ in comments.py:

@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 Extra fields I have defined in my custom comments model:

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

Related form created following 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

Solution

  • Solved by changing the value of the content_type value to users.MyUser

    apps.get_model requires an app_name and a model_name