无效文档错误:创建"源"实例的数据无效。syncStatus - 'datetime.datetime' 对象没有属性 'strip'



在尝试查询下面描述的集合时,我一直遇到以下问题:

class HistorySync(DynamicEmbeddedDocument):
    status = StringField()
    oldestDate = DateTimeField()
    def __init__(self, *args, **kwargs):
        """Overload constructor to deal with history sync status documents"""
        if 'oldestDate' in kwargs:
            kwargs['oldestDate'] = dateparser.parse(kwargs['oldestDate'])
        super(DynamicEmbeddedDocument, self).__init__(*args, **kwargs)

class SyncStatus(DynamicEmbeddedDocument):
    status = StringField()
    synchedAt = DateTimeField()
    newestDate = DateTimeField()
    def __init__(self, *args, **kwargs):
        """Overload constructor to deal with most recent sync status documents"""
        if 'synchedAt' in kwargs:
            kwargs['synchedAt'] = dateparser.parse(kwargs['synchedAt'])
        if 'newestDate' in kwargs:
            kwargs['newestDate'] = dateparser.parse(kwargs['newestDate'])
        super(DynamicEmbeddedDocument, self).__init__(*args, **kwargs)
class Source(UtilityDocument, DynamicDocument):
    source = StringField(required=True)
    sourceName = StringField(required=True)
    supportedDataTypes = ListField(StringField())
    devices = ListField(StringField())
    connectedSince = DateTimeField()
    externalId = StringField()
    historySync = EmbeddedDocumentField(HistorySync)
    syncStatus = EmbeddedDocumentField(SyncStatus)

主文档(Source)可以在我的整个web应用程序中保存良好,但每当我尝试访问带有等语句的Source文档时

Source.objects.first()

我得到以下错误:

    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "/Library/Python/2.7/site-packages/mongoengine/queryset/base.py", line 264, in first
        result = queryset[0]
      File "/Library/Python/2.7/site-packages/mongoengine/queryset/base.py", line 163, in __getitem__
        only_fields=self.only_fields)
      File "/Library/Python/2.7/site-packages/mongoengine/base/document.py", line 725, in _from_son
        raise InvalidDocumentError(msg)
    InvalidDocumentError: Invalid data to create a `Source` instance.
    syncStatus - 'datetime.datetime' object has no attribute 'strip'

非常感谢您的帮助。谢谢

您可以使用

class Source(UtilityDocument, DynamicDocument):
    syncStatus = EmbeddedDocumentField(SyncStatus)
    def __init__(self,*args,**kwargs):
         super(DynamicDocument, self).__init__(*args, **kwargs)
    def __str__(self):` 
        return str(self.syncStatus)
    @property
    def syncStatus(self):
        return str(self.syncStatus)  

返回时将属性转换为字符串。

相关内容

  • 没有找到相关文章

最新更新