Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django中序列化对象时的AssertionError_Python_Django_Django Rest Framework - Fatal编程技术网

Python Django中序列化对象时的AssertionError

Python Django中序列化对象时的AssertionError,python,django,django-rest-framework,Python,Django,Django Rest Framework,在Django中,我想通过Rest序列化一个选择器对象,如下所示: from django.db import models from django.contrib.auth.models import User class Lecteur(User): bibliotheque = models.ManyToManyField('bibliotheque.Livre') 和序列化程序: from rest_framework import serializers from .mod

在Django中,我想通过Rest序列化一个选择器对象,如下所示:

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

class Lecteur(User):
    bibliotheque = models.ManyToManyField('bibliotheque.Livre')
和序列化程序:

from rest_framework import serializers
from .models import Lecteur
from bibliotheque.serializers import LivreSerializer

class LecteurSerializer(serializers.ModelSerializer):
    bibliotheque = LivreSerializer(source='bibliotheque')
    class Meta:
        model=Lecteur
        fields = ('bibliotheque')
问题是,当我评估这一行时,我有一个断言错误 serializer=LecteurSerializerlecteur在我的视图中:

@csrf_exempt
def bibliotheque(request, id):
    """
    Show user's bibliotheque \w id    
    """
    try:
        lecteur = Lecteur.objects.get(id=id)
    except Lecteur.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = LecteurSerializer(lecteur)
        return JSONResponse(serializer.data)

首先,您应该提供完整的回溯,以便我们可以在不重新创建所有环境的情况下深入研究问题。这是常识

问题是:

fields = ('bibliotheque')
正如断言错误所说,字段应该是list或tuple,或者其他iterable

在这里,您应该理解一件事:Python元组语法与操作顺序语法冲突。 所以,'a'='a'。要创建一个元素的元组,应该使用hack:'a',。注意逗号

因此,如果您的情况是这样,解决方案将是:

fields = ('bibliotheque',)

谢谢你的回答,但这不起作用。这是完整的回溯:@Totem那么你能提供你错误的完整回溯吗?我已经编辑了我的第一条评论,你注意到Pastbin链接了吗?如果是,我可以添加什么来让您全面了解项目?