简体   繁体   English

Django REST 错误:TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是“BoundField”

[英]Django REST error: TypeError: int() argument must be a string, a bytes-like object or a number, not 'BoundField'

I have a function which outputs Friday by taking in week number as a parameter我有一个函数,它通过将周数作为参数来输出星期五

def fridayOfWeek(p_week):
    p_year=2021
    monday = datetime.datetime.strptime(f'{p_year}-W{int(p_week )- 1}-1', "%Y-W%W-%w").date()
    wednesday = monday + datetime.timedelta(days=2)
    friday  = monday + datetime.timedelta(days=6.9) - datetime.timedelta(days=2)
    return friday

but when I serializer data and Set但是当我序列化数据并设置时

serialize = ExamSimulatorSerializer(request.data)
date = fridayOfWeek(serializer["week"])

it gives me an error它给了我一个错误

   
      File "C:\Users\user1\Desktop\backend\data_manager\views.py", line 43, in examSimulator
        date = fridayOfWeek(m['week'])
      File "C:\Users\user1\Desktop\backend\week_into_date.py", line 10, in fridayOfWeek
        monday = datetime.datetime.strptime(f'{p_year}-W{int(p_week )- 1}-1', "%Y-W%W-%w").date()
    
    Exception Type: TypeError at /api/exam-simulator/
    Exception Value: int() argument must be a string, a bytes-like object or a number, not 'BoundField'

my serializer class is:我的序列化器类是:

class ExamSimulatorSerializer(serializers.Serializer):
    week = serializers.IntegerField() 
    subject = serializers.CharField()
    code = serializers.CharField()
    
    def create(self, validated_data):
        pass

my views.py我的意见.py

@api_view(['POST'])
@parser_classes([JSONParser])
def examSimulator(request):
    m = ExamSimulatorSerializer(request.data)
    code = m['code']
    date = fridayOfWeek(m['week'])
    subject = Subject.objects.get(name = m['subject'])
    for s in subject.students.all().values():
        student = Student.objects.get(roll_num=s['roll_num'])
        score = random.randint(25,95)
        Exam.objects.create(code=code, date=date, student=student, subject=subject,score=score)
        return Response(status=status.HTTP_200_OK)        

Moreover Is this the right way to make a model-less serializer class此外,这是制作无模型序列化程序类的正确方法吗

you will be able to access the serializer data inside serializer.data["week"] instead of serializer["week"]您将能够访问serializer.data["week"]而不是serializer["week"]的序列化程序数据

also note you have a small typo on the serializer variable, try this:还要注意你在序列化器变量上有一个小错字,试试这个:

serializer = ExamSimulatorSerializer(request.data)
date = fridayOfWeek(serializer.data["week"])

or even:甚至:

serializer = ExamSimulatorSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
date = fridayOfWeek(serializer.validated_data["week"])

if you want to validate your data and raise an exception in case the serializer is not valid如果您想验证您的数据并在序列化程序无效的情况下引发异常

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django - /statements/ int() 参数处的 TypeError 必须是字符串、类似字节的对象或数字,而不是“AnonymousUser” - Django - TypeError at /statements/ int() argument must be a string, a bytes-like object or a number, not 'AnonymousUser' Django: TypeError: int() argument must be a string, a bytes-like object or a number, not 'ObjectId' - Django: TypeError: int() argument must be a string, a bytes-like object or a number, not 'ObjectId' Django TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是'list' - Django TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' Django: TypeError: int() argument must be a string, a bytes-like object or a number, not 'Driver' - Django : TypeError: int() argument must be a string, a bytes-like object or a number, not 'Driver' Django保存到数据库:TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“元组” - Django save to DB: TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple' Django: TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是 - Django: TypeError: int() argument must be a string, a bytes-like object or a number, not django-rest-framework:int()参数必须是字符串,类似字节的对象或数字,而不是Deferred属性 - django-rest-framework: int() argument must be a string, a bytes-like object or a number, not Deferred Attribute TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是'NoneType'错误 - TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' error 类型错误:将形状转换为 TensorShape 时出错:int() 参数必须是字符串、类似字节的对象或数字,而不是“元组”。 在蟒蛇 - TypeError: Error converting shape to a TensorShape: int() argument must be a string, a bytes-like object or a number, not 'tuple'. in python TypeError:int()参数必须是字符串,类字节对象或数字错误 - TypeError: int() argument must be a string, a bytes-like object or a number error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM