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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With