InvalidDocumentError:创建“源”实例的无效数据。 SYNCSTATUS - “datetime.datetime”对象有没有属性“带”
问题描述:
我不断收到试图查询下面描述的集合,当以下问题:InvalidDocumentError:创建“源”实例的无效数据。 SYNCSTATUS - “datetime.datetime”对象有没有属性“带”
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)
的主要文件(来源)能够在整个救细我的web应用程序,但每当我试图用语句如
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)
这将返回当属性转换为字符串。
感谢您的帮助。不幸的是,它不适合我,因为它不允许我初始化文档。我会继续寻找答案,并会很乐意尝试其他任何事情。 – no8do
尝试使用自定义的'def __init __(self,* args,** kwargs):'初始化父类并为子初始化器提供任何自定义逻辑。 – dmitryro