3

I'm using Django REST Framework for the first time on a project and I'm struggling with one particular part. The project is an issue tracker-type applicaton and it uses django_comments to allow for comments on issues. I'm now building an API on top of it to allow for creating a mobile app.

I've created the following serializers:

from django.contrib.auth.models import User
from todolist.models import Project, Issue
from rest_framework import serializers
from django_comments.models import Comment

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'email', 'first_name', 'last_name')


class ProjectSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Project
        fields = ('name', 'description', 'owner')


class CommentSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Comment


class IssueSerializer(serializers.HyperlinkedModelSerializer):
    comments = CommentSerializer(many=True)

    class Meta:
        model = Issue

And here's my project-wide urls.py, where I define the routes:

from django.conf.urls import patterns, include, url 
from todolist.views import HomeTemplateView, UserViewSet, ProjectViewSet, IssueViewSet, CommentViewSet
from rest_framework import routers
from rest_framework.authtoken.views import obtain_auth_token

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'projects', ProjectViewSet)
router.register(r'issues', IssueViewSet)
router.register(r'comments', CommentViewSet)

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'projectile.views.home', name='home'),
    # url(r'^projectile/', include('projectile.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),

    # Comments
    (r'^comments/', include('django_comments.urls')),

    # Login
    url(r'^accounts/login/$', 'django.contrib.auth.views.login'),

    # Logout
    url(r'^accounts/logout/$', 'django.contrib.auth.views.logout_then_login', {'login_url': '/accounts/login/'}),

    # To-do list
    url(r'^projects/', include('todolist.urls')),

    # Home page
    url(r'^$', HomeTemplateView.as_view(
        template_name="todolist/home.html",
        )), 

    # Router URLs
    url(r'^api/', include(router.urls)),

    # REST framework auth URLs
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^api-token-auth/', obtain_auth_token),

    # API docs
    url(r'^docs/', include('rest_framework_swagger.urls'))
)

I'm seeing the following error when I run a test which tries to get all the comments:

ImproperlyConfigured: Could not resolve URL for hyperlinked relationship using view name "contenttype-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.

Also, when I fetch an issue, it doesn't include the comments.

Now, this error appears to me to be that when fetching a comment, it's unable to match up the comment to its parent. I've experienced similar problems when using Tastypie in the past, and I'm unsure how to resolve it with DRF.

Community
  • 1
  • 1
Matthew Daly
  • 9,212
  • 2
  • 42
  • 83
  • can you post your urls.py file. – levi Feb 06 '15 at 15:47
  • do you have a serializer called `contenttype` ? – levi Feb 06 '15 at 15:52
  • @levi No. `django_comments` is the old Django comment system, which was moved to a separate app as of 1.6. It's built using the Django contenttypes framework. The contenttype field is a foreign key to the ContentType model from the contenttypes framework. – Matthew Daly Feb 06 '15 at 15:57
  • check my answer, you need to exclude contenttype field. – levi Feb 06 '15 at 16:04

1 Answers1

4

In your Comment serializer, specify comment instances fields explicitly and exclude field contenttype.

class CommentSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Comment
        fields = ('field_1','field_2','field_3') #here goes your comment fields and dont include contenttype field

The problem is that DRF will try to apply hyperlinks to represent all relationships, and for contenttype field you don't have a view or a serializer and you dont need it either.

levi
  • 22,001
  • 7
  • 73
  • 74