I have a django 1.9 project and am using mongoengine to connect to mongodb instance.
I have a model as follows:
from django.db import models
from mongoengine import *
class Employee(Document):
email = StringField(required=True, unique=True)
first_name = StringField(max_length=50)
last_name = StringField(max_length=50)
A test class:
import web.models as models
class test:
'''Test the accessibility of the business class'''
def testBusiness():
employee = models.Employee.objects.create(
email="pedro.kong@company.com",
first_name="Pedro",
last_name="Kong"
)
try:
employee.save()
except pymongo.errors.DuplicateKeyError as e:
return "Error"
return "Business Tested"
The problem is that i cannot catch the DuplicateKeyError
execption:
pymongo.errors.DuplicateKeyError: E11000 duplicate key error index: rockynode.employee.$email_1 dup key: { : "pedro.kong@company.com" }
I tried with global try except but it still crashes the code with a DuplicateKeyError
exception:
try:
employee.save()
except:
return "Error"
return "Business Tested"
For info am using the following dependencies:
Django==1.9.7
mongoengine==0.10.6
pymongo==3.2.2
Reff:get-the-duplicate-value-on-duplicatekeyerror
Any help would be highly appreciable.Thx.