[Django]-Isinstance() and type() equivelence failure due to import mechanism (python/django)

7👍

isinstance also compare module location, response.context['form'] class’ module is forms where SomeForm module is app.forms you check this by inspecting respectively __class__.__module__ and __module__.

To make isinstance work you can:

  • fix the import in the views.py (recommended)
  • alter sys.path in form_testse.py to be able to import the form as from forms import SomeForm
  • try intrapackage references

0👍

one possible hack is to check the __name__ attribute of the types, although unless you fix it the Right way, you might run into other issues

def sharetypename(obj1, obj2):
    if isinstance(obj1, type):
        c1 = obj1.__name__
    else:
        c1 = type(obj1).__name__

    if isinstance(obj2, type):
        c2 = obj2.__name__
    else:
        c2 = type(obj2).__name__

    return c1 == c2
👤eqzx

Leave a comment