Python and django newbie question, here is code:
class Client(User)
#some fields
client=Client()
client.save()
user=User.objects.all()[0]
#want to ckeck type of user, expect that it is Client:
isinstance(user,Client) #returns false
#but this works:
isinstance(user,User) #returns true
But I expect that user is Client. What’s wrong?
Addition:
Need to get subclass object first, and it works fine:
if hasattr(user,'client'):
client=user.client
Addition 2: Guys, you so angry so I afraid to post anything more in this question 🙂 It’s closed, and I fully understood my absolutely vacuum in knowledge of django, python, polymorphism and other IT technologies, thanks ))
The problem is that Django’s querysets aren’t aware of inheritance. So when you query on User, it won’t ever return the subclasses.
The easiest solution is to do something like this:
Of course, if you have multiple subclasses of User, this gets slightly more complicated. There are ways of getting Django to do model inheritance properly, but they’re all a little bit hacky.