darkhorse
darkhorse

Reputation: 8782

TypeError: __init__() got multiple values for keyword argument 'view_name'

I am using the Django Rest Framework, and I am not sure why I am getting this error.

models.py

from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    followers = models.ManyToManyField('self', related_name='followees', symmetrical=False)

class Post(models.Model):
    author = models.ForeignKey(User, related_name = 'posts')
    title = models.CharField(max_length = 255)
    body = models.TextField(blank = True, null = True)

class Photo(models.Model):
    post = models.ForeignKey(Post, related_name = 'photos')
    image = models.ImageField(upload_to = '%Y/%m/%d')

serializers.py

from rest_framework import serializers

from .models import *

class UserSerializer(serializers.ModelSerializer):
    # Getting the list of posts made by particular users using the username.
    posts = serializers.HyperlinkedIdentityField(
        'posts',
        view_name = 'userpost-list',
        lookup_field = 'username'
    )

    class Meta:
        model = User
        fields = ('id', 'username', 'first_name', 'last_name', 'posts',)

class PostSerializer(serializers.ModelSerializer):
    author = UserSerializer(required = False)
    photos = serializers.HyperlinkedIdentityField(
        'photos',
        view_name = 'postphoto-list'
    )

    def get_validated_exclusions(self):
        # Need to exclude 'author' since we'll add that later
        # based off the request user
        exclusions = super(PostSerializer, self).get_validated_exclusions()
        return exclusions + ['author']

    class Meta:
        model = Post

class PhotoSerializer(serializers.ModelSerializer):
    image = serializers.Field('image.url')

    class Meta:
        model = Photo

views.py

from rest_framework import generics, permissions

from .serializers import *
from .models import *

class UserList(generics.ListCreateAPIView):
    model = User
    serializer_class = UserSerializer
    permission_classes = [
        permissions.AllowAny # Publically available to anyone
    ]

class UserDetail(generics.RetrieveAPIView):
    model = User
    serializer_class = UserSerializer
    lookup_field = 'username'

class PostList(generics.ListCreateAPIView):
    model = Post
    serializer_class = PostSerializer
    permission_classes = [
        permissions.AllowAny
    ]

class PostDetail(generics.RetrieveAPIView):
    model = Post
    serializer_class = PostSerializer
    permission_classes = [
        permissions.AllowAny
    ]

class UserPostList(generics.ListAPIView):
    """
    Lists all the posts of a particular User.
    """
    model = Post
    serializer_class = PostSerializer

    def get_queryset(self):
        queryset = super(UserPostList, self).get_queryset()
        return queryset.filter(author__username = self.kwargs.get('username'))

class PhotoList(generics.ListCreateAPIView):
    model = Photo
    serializer_class = PhotoSerializer
    permission_classes = [
        permissions.AllowAny
    ]

class PhotoDetail(generics.RetrieveAPIView):
    model = Photo
    serializer_class = PhotoSerializer
    permission_classes = [
        permissions.AllowAny
    ]

class PostPhotoList(generics.ListAPIView):
    model = Photo
    serializer_class = PhotoSerializer

    def get_queryset(self):
        queryset = super(PostPhotoList, self).get_queryset()
        return queryset.filter(post__pk = self.kwargs.get('pk'))

urls.py in my app directory

from django.conf.urls import patterns, url, include

from .views import *

urlpatterns = [
    # User URLs
    url(r'^users/$', UserList.as_view(), name='user-list'),
    url(r'^users/(?P<username>[0-9a-zA-Z_-]+)/$', UserDetail.as_view(), name='user-detail'),
    url(r'^users/(?P<username>[0-9a-zA-Z_-]+)/posts/$', UserPostList.as_view(), name='userpost-list'),

    # Post URLs
    url(r'^posts/$', PostList.as_view(), name='post-list'),
    url(r'^posts/(?P<pk>\d+)/$', PostDetail.as_view(), name='post-detail'),
    url(r'^posts/(?P<pk>\d+)/photos/$', PostPhotoList.as_view(), name='postphoto-list'),

    # Photo URLs
    url(r'^photos/$', PhotoList.as_view(), name='photo-list'),
    url(r'^photos/(?P<pk>\d+)/$', PhotoDetail.as_view(), name='photo-detail'),
]

When I try to run the check command on my terminal, or runserver, I get this error: TypeError: init() got multiple values for keyword argument 'view_name'

What am I doing wrong exactly, and how can I fix this problem?

Upvotes: 2

Views: 3134

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600051

The first argument to HyperlinkedIdentityField is view_name. You're passing an extra initial argument, which seems to be the same as the field name; remove this argument.

Upvotes: 5

Related Questions