gpt4 book ai didi

django - 如何在序列化器 Django 中序列化 BigIntegerField、TextField

转载 作者:行者123 更新时间:2023-12-04 10:23:53 25 4
gpt4 key购买 nike

我有一个具有以下属性的模型

from django.db import models
class ApiLogs(models.Model):
user_id = models.BigIntegerField(null=True)
ip = models.CharField(max_length=16)
user_agent = models.TextField(blank=True, null=True)
client = models.CharField(max_length=50, blank=True, null=True)
client_version = models.CharField(max_length=50, blank=True, null=True)
token = models.TextField(blank=True, null=True)
uri = models.CharField(max_length=200)
method = models.CharField(max_length=20)

我已经定义了一个序列化程序
from rest_framework import serializers
class ApiSerializer(serializers.Serializer):
user_id = serializers.BigIntegerField( allow_null=True)
ip = serializers.CharField(max_length=16)
user_agent = serializers.TextField(allow_blank=True, allow_null=True)
client = serializers.CharField(max_length=50, allow_blank=True, allow_null=True)
client_version = serializers.CharField(max_length=50, allow_blank=True, allow_null=True)
token = serializers.TextField(allow_blank=True, allow_null=True)
uri = serializers.CharField(max_length=200)
method = serializers.CharField(max_length=20)

但它显示的错误有点像这样
user_id = serializers.BigIntegerField( allow_null=True)
AttributeError: 'module' object has no attribute 'BigIntegerField'

用于文本字段
user_agent = serializers.TextField(allow_blank=True, allow_null=True)
AttributeError: 'module' object has no attribute 'TextField'

现在如何序列化这种类型的数据。

最佳答案

这是因为 django rest 框架的 Serializer没有 TextField。如果您的模型具有 TextField,您需要在序列化程序中使用 CharField。

CharField A text representation. Optionally validates the text to be shorter than max_length and longer than min_length.

Corresponds to django.db.models.fields.CharField or django.db.models.fields.TextField.



文档对 BigIntegerFields 不太清楚来自模型,但这个 line for the source code表明 IntegerField 再次是您必须在序列化程序中使用的内容。

关于django - 如何在序列化器 Django 中序列化 BigIntegerField、TextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38849201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com