0

I am trying to query the mongodb database using the mongoengine in django

Here's the code:

Document:

class User(Document):
    name = StringField(max_length=60, required=True)

    meta = {
        'collection': 'users'
    }

and when I try to query the user with the random string as id, It throws error, but I want None or user object:

User.objects(id='3sdfs')

Here's the error I'm getting

raise ValidationError(message, errors=errors, field_name=field_name)
mongoengine.errors.ValidationError: '3sdfs' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string
Rohit Khatri
  • 1,980
  • 3
  • 25
  • 45
  • Your id should be a 12-byte input or a 24-character hex string.Yyour request should be `User.objects(name='3sdfs')` ? – Wilfried Mar 25 '17 at 10:49
  • Yes, I know. But I want to make it silent while fetching, because user can pass anything, so I need to return None, I can do try catch, but I don't like to put try catch, because there are more than 50 places I will have to do that, so I am looking for a solution inside the mongoengine itself, any flag to silent the validation error for id only. – Rohit Khatri Mar 25 '17 at 11:11

0 Answers0