Hi,
First of all thanks for the Django framework. It's fun to work with it.
My question: I have a CommonInfo(models.Model) and I do subclassing it for a
couple of models. Getting CommonInfo.objects.all() returns a list of all
objects of CommonInfo and it's subclasses. Q: How to determine what subclass the
object was instantiated of?
Example:
my models.py contains:
class CommonInfo(models.Model):
name = models.CharField(max_length=100)
age = models.PositiveIntegerField()
class Student(CommonInfo):
home_group = models.CharField(max_length=5)
class Teacher(CommonInfo):
level = models.CharField(max_length=5)
>>> ci = CommonInfo(name="commoninfo", age=1)
>>> student = Student(name="student", age=2, home_group="AAA")
>>> teacher = Teacher(name="teacher", age=3, level="B")
[... .save() all objects ...]
>>> for object in CommonInfo.objects.all():
... if hasattr(object, 'home_group'):
... print "found student"
Here I expected to find one object or say the if to print "found
student" one time.
Given the pure Python example:
module aaa.py:
class Foo(object):
objects = []
def __init__(self):
self.objects.append(self)
class Bar(Foo):
fnord = 5
I can do:
>>> foo = Foo()
>>> bar = Bar()
>>> Foo.objects
[<aaa.Foo object at 0x7fed08b7a750>, <aaa.Bar object at 0x7fed08b7aa90>]
>>> for object in Foo.objects:
... if hasattr(object, 'fnord'):
... print "bar"
...
bar
So, even if that might not be possible considering the SQL-DB behind
the ORM: What is the best way to determine the sub class?
Thanks,
Frank
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---