0

Django がモデル フィールド属性db_tableを処理する方法と同様に、継承されたすべてのクラスに名前が含まれるように、基本クラスに Meta クラス属性を設定したいと考えています。related_name

class BaseModel(models.Model):
    class Meta:
        db_table = 'prefix_%(class)s'

したがって、継承されたモデル:

class SubModel(BaseModel):
    pass

db table がありますprefix_submodel

それは可能ですか?Meta クラスは継承クラスのモデル名にアクセスできますか?

4

1 に答える 1

1

いいえ、できません。複数のクラスに同じテーブルを格納するのは簡単ではありません。

必要なのは、おそらくdjeneralizeプロジェクトです。

例から:

class Fruit(BaseGeneralizedModel):
   name = models.CharField(max_length=30)

   def __unicode__(self):
       return self.name

class Apple(Fruit):
   radius = models.IntegerField()

   class Meta:
       specialization = 'apple'

class Banana(Fruit):
   curvature = models.DecimalField(max_digits=3, decimal_places=2)

   class Meta:
       specialization = 'banana'

class Clementine(Fruit):
   pips = models.BooleanField(default=True)

   class Meta:
       specialization = 'clementine'

which then allows the following queries to be executed:

>>> Fruit.objects.all() # what we've got at the moment
[<Fruit: Rosy apple>, <Fruit: Bendy banana>, <Fruit: Sweet
clementine>]
>>> Fruit.specializations.all() # the new stuff!
[<Apple: Rosy apple>, <Banana: Bendy banana>, <Clementine: Sweet
clementine>]
于 2011-01-25T14:29:31.783 に答える