Reputation: 14724
(using dajngo 1.9 and python 2.7)
So I have an abstract class with a few child classes. It looks like this :
class Node(models.Model):
[...]
class Meta:
abstract = True
class Individual(Node):
[...]
class Company(Node):
[...]
class Media(Node):
[...]
class Share(models.Model):
share = models.FloatField(null=True)
child_content_type = models.ForeignKey(ContentType, related_name='child')
child_object_id = models.PositiveIntegerField()
child = GenericForeignKey('child_content_type', 'child_object_id')
parent_content_type = models.ForeignKey(ContentType, related_name='parent')
parent_object_id = models.PositiveIntegerField()
parent = GenericForeignKey('parent_content_type', 'parent_object_id')
The thing is that, virtually, the Share's child and parent can take any model in. So I want to implement an extended save() method (for Share) that will check that both the child and the parent inherit from Node.
I'm looking for something that could look like this :
assert child.inherited_class.name == 'node'
assert parent.inherited_class.name == 'node'
(or with child_content_type
of ocurse)
Upvotes: 1
Views: 245
Reputation: 81604
Use isinstance
:
assert isinstance(child, Node)
assert isinstance(parent, Node)
Upvotes: 2