Home > Mobile >  Django: how to test type of RelatedManager?
Django: how to test type of RelatedManager?

Time:12-15

How to test the type of a RelatedManager in Django ?

assert type(qs) in [models.QuerySet, models.Manager]

fails if qs is a RelatedManager

how do I test if qs is a RelatedManager ?

CodePudding user response:

You can check the type of a related manager object by entering type(some_related_manager_object) in the shell. This returns <class 'django.db.models.manager.Manager'>.

To check if a queryset has type related manager object:

from django.db.models.manager import Manager

assert type(qs)==Manager

CodePudding user response:

ok, found a solution:

assert isinstance(qs, (models.QuerySet, models.Manager))
  • Related