2016-08-13 124 views
1

我想運行一個應用程序,它使用django rest framework.However是得到導入錯誤「無法導入名稱DurationField」。如何解決此錯誤?無法導入名稱DurationField

錯誤消息 enter image description here

Views.py

from django.shortcuts import render 
from django.contrib.auth.models import User 
from django.http import Http404 

from restapp.serializers import UserSerializer 
from rest_framework.views import APIView 
from rest_framework.response import Response 
from rest_framework import status 


class UserList(APIView): 

#List all user,create new one 
def get(self,request,format=None): 
    users = User.objects.all() 
    serializer = UserSerializer(users,many=True) 
    return Response(serializer.data) 

def post(self,request,format=None): 
    serializer= UserSerializer(data=request.DATA) 
    if serializer.is_valid(): 
     serializer.save() 
     return Response(serializer.data, status=status.HTTP_201_CREATED) 
    return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST) 

def delete(self,request,pk, format=None): 
    user = self.get_object(pk) 
    user.delete() 
    return Response(status=status.HTTP_204_NO_CONTENT) 

class UserDetail(APIView): 

#Retrive update,or delete a user instance 

def get_object(self,pk): 
    try: 
     return User.objects.get(pk=pk) 
    except user.DoesNotExist: 
     raise 404 

def get(self,request,pk,format=None): 
    user = self.get_object(pk) 
    user = UserSerializer(user) 
    return Response(user.data) 

def put(self,request,pk,format=None): 
    user = self.get_object(pk) 
    serializer=UserSerializer(user,data = request.DATA) 
    if serializer.is_valid(): 
     serializer.save() 
     return Response(serializer.data) 
    return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST) 


def delete(self,request,pk,format=None): 
    user = self.get_object(pk) 
    user.delete() 
    return Response(status=status.HTTP_204_NO_CONTENT) 

serializers.py

from django.contrib.auth.models import User 
from rest_framework import serializers 

class UserSerializer(serializers.ModelSerializer): 
    class Meta: 
    model = User 
    fields = ('id','username','firstname','last_name','email') 

它指向該文件serializers.py,線18作爲誤差的來源。

enter image description here

+2

你在哪裏使用'DurationField' - 無法在代碼中找到。 – masnun

+1

它指向一個附帶django rest框架的文件(serializers.py,第18行)。我添加了sceenshot,看一看。 – brxy

回答

5

從截圖,我可以看到你使用Django 1.7。

由DRF導入的DurationField在Django 1.8中引入。

所以你必須升級你的Django才能使這個版本的DRF工作。