简体   繁体   中英

'ArticleSerializer' object has no attribute 'get'

The article serializer:

class ArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model=Article
        fields='__all__'

settings:

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ]
}

model:

class Article(models.Model):
    title = models.CharField(max_length=50)
    author = models.CharField(max_length=50)
    email=models.EmailField()
    date=models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.title

view function:

def article_list(request):
    if request.method== 'GET':
        article =Article.objects.all()
        serializer = ArticleSerializer(article,many=True)
        return JsonResponse(serializer.data,safe=False)

    if request.method == 'POST':
        data = JSONParser.parse(request)
        serializer = ArticleSerializer(data=data)

        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data,status=201)
        return JsonResponse(serializer.errors,status=400)


i just created a simple serializer but whenever i try to get the response it says that the ArticleSerializer i created has no attribute 'get'. Error

Pass request.data to the serializer. You are parsing the whole request object and passing that instead of the required data.

...
if request.method == 'POST':
    serializer = ArticleSerializer(data=request.data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM