アプリケーションの GUI データを格納するために、次のようなモデルがあります。
class Application(models.Model):
name = models.CharField(max_length=80)
gui = models.ForeignKey(GuiPanel)
class GuiPanel(models.Model):
dimensions = models.CommaSeparatedIntegerField(max_length=16, help_text='Width,Height')
backgroundImage = models.ImageField(upload_to='guiImages/', blank=True)
class GuiComponent(models.Model):
guiPanel = models.ForeignKey(GuiPanel)
position = models.CommaSeparatedIntegerField(max_length=16) # x,y position
controlId = models.IntegerField(blank=True, null=True) # optional
class meta:
abstract = True
class RotaryDial(GuiComponent):
image = models.ImageField(upload_to='guiImages/')
angleRange = models.CommaSeparatedIntegerField(max_length=16, help_text='startAngle,endAngle')
valueRange = models.CommaSeparatedIntegerField(max_length=16, help_text='startVal,endVal)
class Toggle(GuiComponent): # we always just use 0 and 1 for its value
onImage = models.ImageField(upload_to='guiImages/')
offImage = models.ImageField(upload_to='guiImages/')
したがって、各アプリケーションには GuiPanel があり、これには高さ、幅、背景画像があり、暗黙的に一連の GuiComponents (現在は 2 つのサブクラスがありますが、将来はさらに増える可能性があります) があります。
したがって、次のようなものを使用して、すべての GUI データを含むアプリケーションを取得できます。
Application.objects.prefetch_related('gui', 'gui__rotarydial_set', 'gui__toggle_set').get(pk=1)
約 6 つのサブクラスがあり、将来さらに追加する可能性があるため、これは少し面倒です。
または、GuiPanel に非抽象モデルを使用した場合は、次のようにすることができます。
Application.objects.prefetch_related('gui', 'gui__guicomponent_set').get(pk=1)
次に、hasattr を使用して、guicomponent_set 内の各インスタンスの実際のサブクラスを調べることができます。
まず、全体的なモデルは大丈夫ですか、それとも改善できるでしょうか? そして最後に、この場合、上記のオプション (抽象と非抽象) のどちらが優れているでしょうか?