Mongoengine 保持创建空值键,即使值为 None

问题描述 投票:0回答:0

这是我的主要代码

from mongoengine import *
from models import Video
from env import ENV

connect(
   db='isysrg_inference',
   host=ENV.DB_HOST,
   port=int(ENV.DB_PORT),
)

# Construct response
video = Video(
               video_id=123,
               filename='foo',
               progress=None,
               bytes_size=1234,
               width=1,
               height=2,
               frame_total=3,
               original_video_path='foo.mp4',
               processed_video_path=None)

print(video.to_json())

这里是视频模型

from mongoengine import IntField, StringField, FloatField
from .BaseModel import BaseModel
class Video(BaseModel):
  meta = {
    'collection': 'video'
  }

  video_id = IntField()
  filename = StringField()
  progress = FloatField(required=True)
  bytes_size = IntField()
  width = IntField()
  height = IntField()
  frame_total = IntField()
  original_video_path = StringField()
  processed_video_path = StringField()

这里是输出:

{"_cls": "Video", "video_id": 123, "filename": "foo", "bytes_size": 1234, "width": 1, "height": 2, "frame_total": 3, "original_video_path": "foo.mp4"}

我预计输出将是:

{"_cls": "Video", "video_id": 123, "filename": "foo", "progress":null,"bytes_size": 1234, "width": 1, "height": 2, "frame_total": 3, "original_video_path": "foo.mp4", "processed_video_path":null}

那么即使我设置了值 None,我如何继续创建

processed_video_path
progress
等键。我只想要一致的数据库格式。

python database mongodb mongoengine
© www.soinside.com 2019 - 2024. All rights reserved.