AttributeError: _auto_id_field Django with MongoDB and MongoEngine

AttributeError: _auto_id_field Django with MongoDB and MongoEngine

我在 Django 中使用 mongoengine 下面是我的模型 class

class MyLocation(EmbeddedDocument): my_id = IntField(required=True) lat = GeoPointField(required=False) updated_date_time = DateTimeField(default=datetime.datetime.utcnow)

我的Views.py

def store_my_location(): loc = MyLocation(1, [30.8993487, -74.0145665]) loc.save()

当我调用上述方法时出现错误 AttributeError: _auto_id_field

请提出解决方案

我建议您在保存位置时使用名称。由于 class 定义不包括您如何放置这些键,因此我们需要使用名称来定义它们。

def store_my_location():
    loc = MyLocation(my_id=1, lat=[30.8993487, -74.0145665])
    loc.save()

这应该有效。

另一种方法是将所有内容写入MyLocation class。

class MyLocation(EmbeddedDocument):
    my_id = IntField(required=True)
    lat = GeoPointField(required=False)
    updated_date_time = DateTimeField(default=datetime.datetime.utcnow)

    def create(my_id,lat):
      location=MyLocation(my_id=my_id,lat=lat)
      location.save()
      return location

def store_my_location():
    loc = MyLocation.create(1,[30.8993487, -74.0145665])