区分父模型的Django继承的孩子 [英] Distinguishing parent model's children with Django inheritance
问题描述
问题是,如果我说,一个SwimProgram模型和使用Program作为其基础的CampProgram模型,发现它们是没有一堆try / except块的问题。我想要的是如下:
program = models.Program.objects.get(id = 15)
如果program.swimprogram:
## do stuff
elif program.campprogram:
## do stuff
else:
##做其他的东西
当然这会抛出DoesNotExist异常。我可以使用更糟糕的try / excepts,或者我可以让Program有一个'type'字段,孩子们在保存时设置。两者都是可行的,但如果任何人有更好的方法,我很好奇。
你尝试过hasattr()吗?如下所示:
if hasattr(program,'swimprogram'):
#...
elif hasattr(program,'campprogram'):
#...
如果你是不确定这种方法,首先尝试在一个简单的测试应用程序。这是两个简单的模型,应该显示它是否适用于您和您使用的django版本(在django-1.1.1中测试)。
class Archive(models.Model):
pub_date = models.DateField()
def __unicode __(self):
return存档:%s %self.pub_date
class ArchiveB(Archive):
def __unicode __(self):
returnArchiveB:%s%self.pub_date
然后在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)#他们看起来像归档对象
(<存档:存档:2010-10-10>,<存档:存档:2011-11-11>)
> hasattr(a,'archiveb')
False
> hasattr(b,'archiveb')#但只有一个可以访问ArchiveB
True
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'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
这篇关于区分父模型的Django继承的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!