Distinguishing parent model's children with Django inheritance

后端 未结 3 2088
臣服心动
臣服心动 2021-01-24 06:12

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 n

相关标签:
3条回答
  • 2021-01-24 06:34

    // 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!

    0 讨论(0)
  • 2021-01-24 06:39

    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
    
    0 讨论(0)
  • 2021-01-24 06:39

    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.

    0 讨论(0)
提交回复
热议问题