Distinguishing parent model's children with Django inheritance
Basically I have a Base class called "Program". I then have more specific program model types that use Program as a base class. For 99% of my needs, I don开发者_如何学Go't care whether or not a Program is one of the specific child types. Of course there's that 1% of the time that I do want to know if it's one of the children.
The problem is that if I have let's say, a SwimProgram model and a CampProgram model using Program as their base, that it's problematic to find out what they are without a bunch of try/except blocks. What I want is something like the following:
program = models.Program.objects.get(id=15)
if program.swimprogram:
## do stuff
elif program.campprogram:
## do stuff
else:
## do other stuff
Of course this throws DoesNotExist exceptions. I could either use try/excepts which are uglier, or I could have Program have a 'type' field that the children set on save. Both are doable, but I'm curious if anyone has any better methods.
Have you tried hasattr()? Something like this:
if hasattr(program, 'swimprogram'):
# ...
elif hasattr(program, 'campprogram'):
# ...
If you are unsure about this approach, try it out in a simple test app first. Here are two simple models that should show if it will work for you and the version of django that you are using (tested in django-1.1.1).
class Archive(models.Model):
pub_date = models.DateField()
def __unicode__(self):
return "Archive: %s" % self.pub_date
class ArchiveB(Archive):
def __unicode__(self):
return "ArchiveB: %s" % self.pub_date
And then giving it a spin in the shell:
> a_id = Archive.objects.create(pub_date="2010-10-10").id
> b_id = ArchiveB.objects.create(pub_date="2011-11-11").id
> a = Archive.objects.get(id=a_id)
> b = Archive.objects.get(id=b_id)
> (a, b) # they both look like archive objects
(<Archive: Archive: 2010-10-10>, <Archive: Archive: 2011-11-11>)
> hasattr(a, 'archiveb')
False
> hasattr(b, 'archiveb') # but only one has access to an ArchiveB
True
A couple of weeks ago, someone on the django-developers mailing list introduced a very interesting extension to Django's ORM that makes QuerySets return subclassed objects instead of objects of the parent class. You can read all about it here:
http://bserve.webhop.org/wiki/django_polymorphic
I haven't tried it myself yet (but certainly will), but it seems to fit your use case.
// Update
As pointed out in the comments to this post I got the question wrong. The answer below will not solve the problem.
Hi f4nt,
the easiest way I can think of right now would be the following:
program = models.Program.objects.get(id=15)
if program.__class__.__name__ == 'ModelA':
# to something
if program.__class__.__name__ == 'ModelB':
# to something
To make this a bit better you could write a method in the base model:
class MyModel(models.Model):
def instanceOfModel(self, model_name):
return self.__class__.__name__ == model_name
That way the code from above would look like this:
program = models.Program.objects.get(id=15)
if program.instanceOfModel('ModelA'):
# to something
if program.instanceOfModel('ModelB'):
# to something
But as you can imagine this is ugly. You could look into the content type framework which might help you to do the same except more elegent.
Hope that helps!
精彩评论