使用DRF时Django BuiltinSignatureError

6ljaweal  于 2023-10-21  发布在  Go
关注(0)|答案(1)|浏览(147)

我无法找出错误。API有时返回数据,有时不返回。

class CreateQuiz(generics.ListCreateAPIView):    
    serializer_class = QuizSerializer
    
    
    def get_queryset(self):    
        classId = self.kwargs.get('classId',None)
        subject = self.kwargs.get('subject',None)
        category = Category.objects.filter(class_id=classId,category=subject).values_list('id',flat=True)[0]        
        # chapter = self.kwargs.get('chapter',None)
        
        subcategory=self.kwargs.get('chapter',None)
        subcat = SubCategory.objects.filter(id=subcategory).values_list('sub_category',flat=True)[0]
        total_marks = 30
        questionIDs = Question.objects.raw('''somesql''',params=[category,subcategory,total_marks,category,subcategory,total_marks])
        #
        
        # and qq.sub_category_id IN %s
        questions= MCQuestion.objects.filter(question_ptr_id__in=questionIDs).prefetch_related('answer_set__question')
        essayquestions= Essay_Question.objects.filter(question_ptr_id__in=questionIDs)
        
        user = User.objects.get(id=1)
        
        if MCQuestion.objects.filter(question_ptr_id__in=questionIDs).prefetch_related('answer_set__question').exists():
                    print("create quiz")
                    
                    quiz = Quiz()                        
                    
                    quiz.durationtest="10:00"
                    quiz.random_order=True
                    quiz.save()
                    
                    
                    quizid = Quiz.objects.filter(id=quiz.id).values_list('id',flat=True)[0]      
                    quiz = Quiz.objects.get(id=quiz.id)
                    quiz.title = "Practice Exam : "+ quiz.category.class_id.classVal +" "+quiz.category.category +" ("+subcat+") "
                    quiz.save()
                     
                    with connection.cursor() as cursor:
                                cursor.execute("INSERT INTO quiz_subcategory_quiz(subcategory_id,quiz_id) VALUES (%s,%s)",(subcategory,quiz.id,))
                    for obj in questionIDs:                           
                         
                        
                        if Question.objects.filter(id=obj.id,quiz__id=quiz.id).exists():
                            print("Do nothing")
                        else:
                            question = Question(id=obj.id)                        
                            question.quiz.add(quiz.id)
                            question.save()
                        
                       
                    obj, created = UserQuiz.objects.update_or_create(
                            user_id = user.id,quiz_id=quiz.id            
                        )
                                        
                    return Quiz.objects.filter(id=quizid)
        else:
            response = JsonResponse({"error": "there was an error"})
            response.status_code = 403 # To announce that the user isn't allowed to publish
            return response    
                       
        return Quiz.objects.filter(id=quizid)

我的序列化程序类

class QuizSerializer(serializers.ModelSerializer):  
    
    class Meta:
        model = Quiz
        fields=(
            'id',
            'title',
            'description',
            'url',
            'category',
            'random_order',
            'pass_mark',
            'draft',
            'durationtest',               
            
        )

我得到以下错误,它随机发生。有什么建议请。

QuizSerializer.title的字段源Map到内置函数类型,无效。在bytes示例上定义一个属性或方法, Package 对内置函数的调用。

czq61nw1

czq61nw11#

我在我的终端上遇到了类似的错误消息。知道这篇文章是2岁,也许我的解决方案将帮助别人在未来。
我的错误消息是说:
Article.title的字段源Map到内置函数类型,因此无效。在str示例上定义一个属性或方法, Package 对内置函数的调用。
问题是我没有传递Article模型的示例给序列化器,我传递的是一个字符串!因此,我假设OP在发生此错误时正在传递bytes类型。

相关问题