1

我试过这个例子:http ://mongoengine-odm.readthedocs.org/en/latest/tutorial.html?highlight=query%20document%20inheritance#posts 但不适合我。

我想存储来自天气 api 的数据: http ://api.met.no/weatherapi/locationforecast/1.8/?lat=46.079001;lon=14.51860012

所以我有这样的models.py

import mongoengine

class Weather(mongoengine.Document):
    created = mongoengine.DateTimeField
    latitude = mongoengine.DecimalField
    longitude = mongoengine.DecimalField
    model_name = mongoengine.StringField

class Precipitation(Weather):
    dateFrom = mongoengine.DateTimeField(required=True)
    dateTo = mongoengine.DateTimeField(required=True)
    precipitation = mongoengine.DecimalField
    symbol = mongoengine.IntField(min_value=1, max_value=23)

class State(Weather):
    temperature = mongoengine.DecimalField
    windDirection = mongoengine.StringField
    windAngle = mongoengine.DecimalField

尝试保存数据,例如:models.State.objects.create(temperature=17)

什么都没有保存!

我知道我在字段定义中缺少构造函数的参数,但我真的不知道如何实现它。

4

1 回答 1

3

您需要类实例,例如:

import mongoengine

class Weather(mongoengine.Document):
    created = mongoengine.DateTimeField()
    latitude = mongoengine.DecimalField()
    longitude = mongoengine.DecimalField()
    model_name = mongoengine.StringField()

class Precipitation(Weather):
    dateFrom = mongoengine.DateTimeField(required=True)
    dateTo = mongoengine.DateTimeField(required=True)
    precipitation = mongoengine.DecimalField
    symbol = mongoengine.IntField(min_value=1, max_value=23)

class State(Weather):
    temperature = mongoengine.DecimalField()
    windDirection = mongoengine.StringField()
    windAngle = mongoengine.DecimalField()
于 2012-09-12T08:32:07.313 回答