danirus / django-comments-xtd

A pluggable Django comments application with thread support, follow-up notifications, mail confirmation, like/dislike flags, moderation, a ReactJS plugin and Bootstrap 5.3.
https://django-comments-xtd.readthedocs.io
BSD 2-Clause "Simplified" License
594 stars 158 forks source link

Queryset is none when try to post new comment. #78

Open vadim-zabolotniy opened 6 years ago

vadim-zabolotniy commented 6 years ago

Traceback: File "/path/to/projects_dir/project/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response

  1. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/path/to/projects_dir/project/venv/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  2. return view_func(*args, **kwargs) File "/path/to/projects_dir/project/venv/lib/python2.7/site-packages/django/views/generic/base.py" in view
  3. return self.dispatch(request, *args, **kwargs) File "/path/to/projects_dir/project/venv/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  4. response = self.handle_exception(exc) File "/path/to/projects_dir/project/venv/lib/python2.7/site-packages/rest_framework/views.py" in handle_exception
  5. self.raise_uncaught_exception(exc) File "/path/to/projects_dir/project/venv/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  6. self.initial(request, *args, **kwargs) File "/path/to/projects_dir/project/venv/lib/python2.7/site-packages/rest_framework/views.py" in initial
  7. self.check_permissions(request) File "/path/to/projects_dir/project/venv/lib/python2.7/site-packages/rest_framework/views.py" in check_permissions
  8. if not permission.has_permission(request, self): File "/path/to/projects_dir/project/venv/lib/python2.7/site-packages/rest_framework/permissions.py" in has_permission
  9. queryset = view.get_queryset() File "/path/to/projects_dir/project/venv/lib/python2.7/site-packages/rest_framework/generics.py" in get_queryset
  10. % self.class.name

Exception Type: AssertionError at /comments/api/comment/ Exception Value: 'CommentCreate' should either include a queryset attribute, or override the get_queryset() method.

Versions: Django (1.8.18) djangorestframework (3.6.4) django-contrib-comments (1.8.0)

Ronster2018 commented 4 years ago

Hello everyone!

I am working with the Web API and using the endpoint "comments/api/comment/" on Django 2.2.3, djangorestframework 3.11.0 and python 3.7.

When navigating to the django admin panel, I am able to create a comment and even nest the comments. When I sent the request through the DRF web page or make a POST request through Postman, I get the following error.

Exception Type: AssertionError Exception Value: 'CommentCreate' should either include a queryset attribute, or override the get_queryset() method. Exception Location: .../lib/python3.7/site-packages/rest_framework/generics.py in get_queryset, line 66

Then, when navigating to the comments API endpoint to view the comments attached to an object at "comments/api/app_label-model/pk" it returns an empty json object. But once I add "/count" to the end of the endpoint, it returns a count of 1 because I was able to add a comment through the django admin.

I am wondering if @vadim-zabolotniy or anyone has figured out this issue? I can recreate the issue and provide more information of necessary.

danirus commented 4 years ago

Hi @Ronster2018, @vadim-zabolotniy,

I just created a setup like yours (python 3.7, DRF 3.11.0 and Django 2.2.3). I successfully post against the example/comp site (see it in the examples directory of the repository) using a REST client (ARC). I can also GET the list of comments posted to a the ContentType articles-article.

I use the sqlite3 in the example/comp site. What database backend do you use?

Ronster2018 commented 4 years ago

Hello,

Thank you for such a speedy response. I am also using an sqlite3 backend as well. I do have a lot of dependencies in this project so there could be a potential clash with them. As a way of testing, I will create an entirely new project in a virtual environment to test out the same functions there. I will give you an update as soon as I am done.

Ronster2018 commented 4 years ago

Good afternoon,

I went ahead and created a completely new project and have the following requirements installed:

asgiref==3.2.7
Django==2.2.3
django-comments-xtd==2.4.2
django-contrib-comments==1.9.2
djangorestframework==3.11.0
docutils==0.16
pytz==2019.3
six==1.14.0
sqlparse==0.3.1

I am still running python 3.7.7

My settings.py file:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'TestAppHere',
    'rest_framework',
    'django_comments_xtd',
    'django_comments',
]

my urls.py thats in the same dir as my settings.py:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('comments/', include('django_comments_xtd.urls')),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]

The app label for the new project is "TestAppHere" and the model name is just "Test" to keep it all simple.

class Test(models.Model): 
    owner = models.ForeignKey('auth.User',
                              null=False,
                              blank=True,
                              # i.e the link objects
                              on_delete=models.CASCADE) 
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=75, unique=True)
    comment_text = models.TextField(max_length=120, null=True, blank=True)
    date_added = models.DateTimeField("Date added", default=datetime.now())

    def __str__(self):
        return self.title

I'm using the endpoint at http://127.0.0.1:8080/comments/api/comment/ to POST a comment as well as the endpoing http://127.0.0.1:8080/comments/api/testapphere-test/1/ to view the comments. Odds are, I'm missing something small. Any help would be appreciated!

danirus commented 4 years ago

So what is Test? Is it like Article in the example/comp directory?

Ronster2018 commented 4 years ago

Correct. Its similar to the structure that the example/com directory. Where you have example/comp/articles/models.py and with in that models.pyyou have an Articles class. I have TestProject/TestProj/TestAppHere/models.py and within that models.py I have a class Test.

I've gone ahead and uploaded the sample that I have been working with to make the visualization a little bit easier. Please take a look when you get the chance.

Update: I managed to display all of the comments though the DRF web api. It turns out that the site_id did not match. Although I have example.com and 127.0.0.1 as the only two sites in the admin panel, the ID for the sites were not 1 and 2. They were 2(example.com) and 3(127.0.0.1). I also changed my app's name to Test and the models name to Test and it looks like I needed to use Test-test instead of test-test as the content-type although the doc's say its supposed to be lowercase. But when I make a post to the comments/api/comment with the content type as Test-test , Test, or test, I still encounter the earlier error:

Request Method: POST Request URL:http://127.0.0.1:9000/comments/api/comment/ Django Version: 2.2.3 Exception type: AssertionError Exception Value: 'CommentCreate' should either include a queryset attribute, or override the get_queryset() method. Exception Location: /Users/kron/Workspace/Test Project/lib/python3.7/site-packages/rest_framework/generics.py in get_queryset, line 66

Test Project.zip

danirus commented 4 years ago

I would prefer if you could put the code in a repository. We can use the collaboration tools of GitHub to nail the issue. Let me know when you have it.

Ronster2018 commented 4 years ago

That sounds like a good idea. I just added it to a quick little repo. Here is the link: https://github.com/Ronster2018/Test-API-Comments.git

danirus commented 4 years ago

@Ronster2018, the project is incomplete. Among the fields needed in the CommentCreate view of the API, you have to pass the timestamp and security_hash fields. Those fields are automatically fed when using the templates, or by using the JavaScript plugin, but you don't have neither of both.

I suggest you to start with an example that is known to work. Just follow [this]() instructions to install the comp example site at the examples/comp directory. Play with the API against that project first.

To POST using the API at http://localhost:8000/comments/api/comment/ pass all the following fields: content_type: articles.article, object_pk: the primary key of the article to which you want post the comment, timestamp and security_hash (find the values by looking into the rendered article's page), honeypot: a blank value, name: Your name, email: an email address, url: can be blank, comment: The actual text of the comment, followup: 0, reply_to: 0.

Let me know the result over here.

Ronster2018 commented 4 years ago

Good afternoon @danirus I went through your tutorials and played around a lot with the app and it has a lot of cool things going on. But once I finally got around with interacting with the api, I noticed that it is very dependent on the security_hash that is being generated by the md5 hash of the form. My project is strictly and api back end and I have nothing on the front end to generate those hashes for me.

At the end of the day, it works with all of the values that I copied and pasted from the front end rendered pages, but if I just have an API in place, is there still a way to post comments?

FabrizioA commented 4 years ago

I have same issue at the same time :) I have a SPA web app so I can add some Django tags in a base.html template... but does it ok to post all comments in all pages of user session?

danirus commented 4 years ago

Yes, that's right, it's indeed missing. The project needs an API point to post comments. I put it in my todo list, along with writing new documentation about how to use the API with other web projects like frontend apps or progressive web applications.

Django is a very stable and powerful backend that is very often used in combination with all sort of JavaScript web artefacts. This application requires a full featured API.

If you have already started a branch to include it feel free to share it, we could work on it together. Otherwise I will do it and share here the branch so that we can test it.