How do I find the “concrete class” of a django model baseclass

后端 未结 5 1657
星月不相逢
星月不相逢 2020-12-14 23:11

I\'m trying to find the actual class of a django-model object, when using model-inheritance.

Some code to describe the problem:

class Base(models.mod         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 23:44

    Well... My problem was. In a view, I had this principal model, lets say "Big_Model" and there were some "Small_Model" related to "Big_Model". So when I wanted to retrieve all "Small_Model" related to a certain instance of "Big_Model" I did that **_set.all() stuff. But the point is that Small_Model has Child Classes and I wanted, in views.py, to get which child class was each of the Small_Model instances related to. My trick was to define boolean methods in model Small_Model like is_child_1() and is_child_2(). And when it is true, you apply the actual child pointer instead of the Small_Model pointer.

    Ok... Thats not clear enough, still I dont have much time to write a good example, so i'll just copy-paste my case here:

    class Cache(models.Model):
      valor = models.DecimalField(max_digits=9, decimal_places=2, blank= True, null= True)
      evento=models.ForeignKey(Evento)
      def __unicode__(self):
        return u'%s: %s' % (self.evento, self.valor)
      class Meta:
        verbose_name='Cachê'
        verbose_name_plural='Cachês'
      def is_cb(self):
        try:
          self.cache_bilheteria
          return True
        except self.DoesNotExist:
          return False
      def is_co(self):
        try:
          self.cache_outro
          return True
        except self.DoesNotExist:
          return False
    

提交回复
热议问题