12

I'm trying to do this:

from django.db.models.fields.related import RelatedManager

because I want to be able to test if an object is a related manager ie:

isinstance(obj, RelatedManager)

however I keep getting this error: Error: cannot import name RelatedManager

9-bits
  • 10,395
  • 21
  • 61
  • 83

1 Answers1

11

The related manager classes are created at runtime inside generator functions in django.db.models.fields.related thus you can't import them directly. If you want to check if an object is a related manager for a specific relation you can use isinstance(obj, MyModel.my_relation.__class__). You could also use hasattr to determine if the object has the properties you need (ducktyping) and avoid using isinstance altogether.

narced133
  • 712
  • 4
  • 7