tags:

views:

45

answers:

2

Something like this:

if self.__class__ == "User":
                logging.debug("%s non_pks were found" %  (str(len(non_pks))) )

In [2]: user = User.objects.get(pk=1)

In [3]: user.__class__
Out[3]: <class 'django.contrib.auth.models.User'>

In [4]: if user.__class__ == 'django.contrib.auth.models.User': print "yes"
   ...: 

In [5]: user.__class__ == 'django.contrib.auth.models.User'
Out[5]: False

In [6]: user.__class__ == 'User'
Out[6]: False

In [7]: user.__class__ == "<class 'django.contrib.auth.models.User'>"
Out[7]: False
+2  A: 

This should work:

if user.__class__.__name__ == 'User':
KillianDS
Why are you checking the name of the class, rather than comparing the class objects themselves? (This works even in the face of same-named classes in different modules, for example)
moshez
Your method is better indeed, I was looking for strings because he actually compared with them (upvoted yours).
KillianDS
+3  A: 

Classes are first class objects in Python:

>>> class Foo(object):
...     pass
... 
>>> a = Foo()
>>> a.__class__ == Foo
True

Note: they're not strings, they're objects. Don't compare to "Foo" but to Foo

moshez