This means that your model does not have a field named key
, which is what it's looking for because you are passing in key=val
.
I'm assuming that you're looking for key
to be replaced by the value of the key
argument to the function. Keyword arguments do not work this way, so in this case you'll need to construct a dictionary and use unpacking:
obj = self.__model__.objects(**{key: val}).first()