我在sql數據庫中有3個表。我需要爲這些創建django模型。Django三個關係表的模型設計幫助
table1: Module
|modulename |moduleversion |notes |
-----------------------------------------
|M1 |1 |module 1 |
|M2 |2 |module 2 |
|M3 |1 |module 3 |
|M4 |1 |module 4 |
|M5 |3 |module 5 |
-----------------------------------------
table2: metamodule
|metamodulename |metaversion |modulename |
---------------------------------------------
|A |1 |M1 |
|B |2 |M1 |
|C |1 |M3 |
|A |1 |M4 |
|B |2 |M2 |
---------------------------------------------
table3: release
|releasenumber |notes |metamodules|
-----------------------------------------
|R1 |Rel ver 1 |A |
|R1 |Rel ver 1 |B |
|R2 |Rel ver 2 |A |
|R2 |Rel ver 2 |C |
|R3 |Rel ver 3 |C |
-----------------------------------------
這些表具有外鍵約束。我很困惑哪個關係模型用於創建上述表格的模型。
解決方案:
from django.db import models
class module(models.Model):
modulename = models.CharField(max_length=50)
moduleversion = models.IntegerField(default=0)
notes = models.CharField(max_length=50)
def __unicode__(self):
return unicode(self.modulename)
class metamodule(models.Model):
metamodulenname = models.CharField(max_length=50)
metaversion = models.IntegerField(default=0)
modulename = models.Foreignkey(module)
def __unicode__(self):
return unicode(self.metamodulename)
class release(models.Model):
releasenumber = models.CharField(max_length=2)
notes = models.CharField(max_length=50)
metamodules = models.ManytoMany(metamodule)
def __unicode__(self):
return unicode(self.releasenumber)
在這裏,我想到一個解決方案。 請放下您的建議和解決方案,以糾正我的解決方案
好的,謝謝。對於我寫的解決方案還有什麼建議?那是對的嗎? – abhi3389