我正在使用Django Rest Framework,我不确定为什么会收到此错误。
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'))
我的app目录中的urls.py
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'),
]
当我尝试在我的终端或runserver上运行check命令时,出现此错误: TypeError: init ()为关键字参数'view_name'
获取了多个值我到底做错了什么,我该如何解决这个问题呢?
答案 0 :(得分:5)
HyperlinkedIdentityField的第一个参数是view_name。您正在传递一个额外的初始参数,该参数似乎与字段名称相同;删除这个论点。