DRF 序列化器的 BooleanField returns null for empty ForeignKey field
DRF serializer's BooleanField returns null for empty ForeignKey field
我想将布尔字段 has_video
和 has_gallery
添加到我的序列化程序。
如果 MyModel(视频、画廊)的 ForeignKey 字段有值,则它们的值应为 true
,否则这些值应设置为 false
。
models.py
class MyModel(models.Model):
video = models.ForeignKey(
to='videos.Video',
null=True,
blank=True,
on_delete=models.SET_NULL,
)
gallery = models.ForeignKey(
to='galleries.Gallery',
null=True,
blank=True,
on_delete=models.SET_NULL,
)
serializers.py
class MyModelSerializer(serializers.ModelSerializer):
has_video = serializers.BooleanField(source='video', default=False)
has_gallery = serializers.BooleanField(source='gallery', default=False)
当 MyModel 对象的视频或画廊值为 null 时出现问题。我希望 returned 值是假的,但它是空的。
"has_video": null,
"has_gallery": null,
我尝试将 allow_null
参数设置为 false
但结果是一样的(值仍然是 null
)。
has_video = serializers.BooleanField(source='video', default=False, allow_null=False)
has_gallery = serializers.BooleanField(source='gallery', default=False, allow_null=False)
当视频或图库不为 null 时,序列化程序的字段 return 如我所料为真。问题只是关于 null/false 个值。
这是我在我的一个项目中采用的方法。
class MyModelSerializer(serializers.ModelSerializer):
has_video = serializers.SerializerMethodField('get_has_video', read_only=True)
has_gallery = serializers.SerializerMethodField(source='get_has_gallery', read_only=True)
# ... Your other fields
class Meta:
model = "Your model name"
fields = ("your model fields",
,"has_video", "has_gallery") # include the above two fields
def get_has_video(self, obj):
# now your object should be having videos then you want True so do this like this
return True if obj.video else False
def get_has_gallery(self, obj):
# now your object should be having galleries then you want True so do this like this
return True if obj.gallery else False
我想将布尔字段 has_video
和 has_gallery
添加到我的序列化程序。
如果 MyModel(视频、画廊)的 ForeignKey 字段有值,则它们的值应为 true
,否则这些值应设置为 false
。
models.py
class MyModel(models.Model):
video = models.ForeignKey(
to='videos.Video',
null=True,
blank=True,
on_delete=models.SET_NULL,
)
gallery = models.ForeignKey(
to='galleries.Gallery',
null=True,
blank=True,
on_delete=models.SET_NULL,
)
serializers.py
class MyModelSerializer(serializers.ModelSerializer):
has_video = serializers.BooleanField(source='video', default=False)
has_gallery = serializers.BooleanField(source='gallery', default=False)
当 MyModel 对象的视频或画廊值为 null 时出现问题。我希望 returned 值是假的,但它是空的。
"has_video": null,
"has_gallery": null,
我尝试将 allow_null
参数设置为 false
但结果是一样的(值仍然是 null
)。
has_video = serializers.BooleanField(source='video', default=False, allow_null=False)
has_gallery = serializers.BooleanField(source='gallery', default=False, allow_null=False)
当视频或图库不为 null 时,序列化程序的字段 return 如我所料为真。问题只是关于 null/false 个值。
这是我在我的一个项目中采用的方法。
class MyModelSerializer(serializers.ModelSerializer):
has_video = serializers.SerializerMethodField('get_has_video', read_only=True)
has_gallery = serializers.SerializerMethodField(source='get_has_gallery', read_only=True)
# ... Your other fields
class Meta:
model = "Your model name"
fields = ("your model fields",
,"has_video", "has_gallery") # include the above two fields
def get_has_video(self, obj):
# now your object should be having videos then you want True so do this like this
return True if obj.video else False
def get_has_gallery(self, obj):
# now your object should be having galleries then you want True so do this like this
return True if obj.gallery else False