Adding fields on demand to a Document
say I have this documents:
from mongoengine import Document, EmbeddedDocument, fields
import datetime
class EmbeddedColumn(EmbeddedDocument):
created = fields.DateTimeField(default=datetime.datetime.now)
class Dattum(Document):
datasource_id = fields.IntField(required=True)
date_modified = fields.DateTimeField(default=datetime.datetime.now)
point = fields.GeoPointField()
columns = fields.ListField(fields.EmbeddedDocumentField(EmbeddedColumn))
At runtime i need to add some field to some instances according to a series of queries:
for row in csv_attach:
dato = Dattum(datasource_id=datasource.pk)
for column in columns:
col_dict = model_to_dict(column)
col_dict.pop('id')
ecol = EmbeddedColumn(**col_dict)
dato.columns.append(ecol)
if ecol.geodata_type=='point':
local_search = gmaps.local_search('%s %s' %(ecol.value, region))
results = local_search['responseData']['results']
result_len =
if len(results) == 1:
result = results[0]
#dato.point(result['lat'], result['lng'])
dato.geojson = geojson.Point(dato.point)
dato.save()
W开发者_如何学编程hen I retrieve some Dattum I see that it has the proper columns, but the no columns has fields from model_to_dict(column) and dato have no attribute geojson.
May be I'm asking too much magic to mongoengine, may be there is a proper way to tackle this. Any pointer?
I agree with dcrosta - use a DictField instead of an EmbeddedDocument - you have no validation on the created date, other than that you get a flexible store for your data, the same as an EmbeddedField.
This feature is not yet available in mongoengine. Although there seems to be dicussion regarding this issue in github
加载中,请稍侯......
精彩评论