Naga
Naga

Reputation: 2021

AttributeError: _auto_id_field Django with MongoDB and MongoEngine

I am using mongoengine with Django Below is my Model class

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

My Views.py

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

When I am calling the above method I getting error AttributeError: _auto_id_field

Please suggest a solution

Upvotes: 0

Views: 91

Answers (1)

user11697799
user11697799

Reputation:

I suggest using the names when you save the location. Since class definition does not include how you put in these keys that is why we need to use the name to define them.

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

This should work.

One more appraoch is to write everything in 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])


Upvotes: 2

Related Questions