title: Mongoengine with django: DuplicateKeyError
body: <p>I'm using mongoengine (<a href="http://mongoengine.org/" rel="nofollow noreferrer">http://mongoengine.org/</a>) in a Django project. I don't know if this is such a good idea, but I thought I'd just try to get it work since there seems to be no up to date MongoDB implementation for Django. When running my server and trying to access <code>localhost:8000/workoutcal/</code> I get this error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
      response = get_response(request)
    File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
      response = self.process_exception_by_middleware(e, request)
    File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/workout/workoutcal/views.py", line 18, in calendar
      workouts_in_month = Workout.objects(Q(date__gte=datetime(year=today_year, month=today_month, day=today_day)) &amp; Q(date__lt=datetime(year=today_year, month=today_month+1, day=1)))
    File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/mongoengine/queryset/manager.py", line 37, in __get__
      queryset = queryset_class(owner, owner._get_collection())
    File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/mongoengine/document.py", line 204, in _get_collection
      cls.ensure_indexes()
    File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/mongoengine/document.py", line 834, in ensure_indexes
      collection.create_index(fields, background=background, **opts)
    File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/pymongo/collection.py", line 1571, in create_index
      self.__create_index(keys, kwargs)
    File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/pymongo/collection.py", line 1472, in __create_index
      parse_write_concern_error=True)
    File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/pymongo/collection.py", line 232, in _command
      collation=collation)
    File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/pymongo/pool.py", line 477, in command
      collation=collation)
    File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/pymongo/network.py", line 116, in command
      parse_write_concern_error=parse_write_concern_error)
    File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/pymongo/helpers.py", line 203, in _check_command_response
      raise DuplicateKeyError(errmsg, code, response)
  pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: db.workout index: id_1 dup key: { : null }
  </code></pre>
  
  <p>I have trouble understanding this error. Does it mean there's a duplicate key for the workout document? That sounds strange since this is what my <strong>workoutcal/models.py</strong> looks like:</p>
  
  <pre><code>from mongoengine import *
  
  class Person(Document):
      name = StringField(max_length = 200)
      person_id = IntField(unique = True)
      def __str__(self):
          return str(self.person_id) + self.name
  
  class Lift(EmbeddedDocument):
      lift_id = IntField(unique=True)
      name = StringField(max_length=200) #e.g. bench press, etc
      sets = ListField(IntField()) # No of reps in each set
      def __str__(self):
          return self.name
  
  class Cardio(EmbeddedDocument):
      cardio_id = IntField(unique=True)
      name = StringField(max_length=200)
      duration = IntField() #Number of minutes
      distance = IntField() #Number of metres
      def __str__(self):
          return self.name
  
  class Workout(Document):
      id = IntField(unique=True)
      date = DateTimeField()
      person = ReferenceField(Person)
      lifts = ListField(EmbeddedDocumentField(Lift))
      cardio = ListField(EmbeddedDocumentField(Cardio))
      def __str__(self):
          return str(self.date)+" "+self.person.name
  </code></pre>
  
  <p>Other info that might be useful:</p>
  
  <p>I did not know where to do <code>connect('db')</code> (connecting to the mongod database on my computer) so I just put it in my <strong>settings.py</strong>:</p>
  
  <pre><code>from mongoengine import connect
  ...
  connect('db') # &lt;---
  DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.sqlite3', # I kept this since it's needed for admin, etc.
          'NAME': 'mydatabase',
      }
  }
  </code></pre>
  
  <p><strong>workoutcal/urls.py:</strong></p>
  
  <pre><code>app_name = 'workoutcal'
  urlpatterns = [
      url(r'^$', views.calendar, name = 'calendar'),
      url(r'^(?P&lt;id&gt;[0-9]+)/$', views.detail, name = 'detail'), #Detail of a workout. Shows lifts, duration, etc. All the attributes.
      url(r'^add/(?P&lt;year&gt;[0-9]+)/(?P&lt;month&gt;[0-9]+)/(?P&lt;day&gt;[0-9]+)/$', views.add, name = 'add'), #Adding a workout for the date in question
      url(r'^edit/(?P&lt;id&gt;[0-9]+)/$', views.edit, name = 'edit'), #Editing an existing workout. id is id of workout object to edit.
  ]
  </code></pre>
  
  <p>In short, my questions are:</p>
  
  <ol>
  <li>Why am I getting this error and what can I do about it?</li>
  <li>What is the correct what to <code>connect()</code> to the <code>mongod</code> in my Django project?</li>
  <li>Your thoughts about integrating mongoenging with django in this way?</li>
  </ol>