2013-12-12 41 views
0

說我有在Django幾個型號:如何加入4或5桌在Django

class LevelZ(models.Model): 
    title = models.CharField(_('yahoo z name'), max_length=255) 
    is_display = models.BooleanField(_('Is displayed'), default=True) 
    enable = models.BooleanField(_('is enable'), default=True) 

class LevelSub(models.Model): 
    levelzs = models.ManyToManyField(LevelZ, verbose_name=_('in levelzs'), through='LevelSubLevelZ', related_name='inlevelzs') 
    title = models.CharField(_('yahoo sub name'), max_length=255) 
    is_display = models.BooleanField(_('Is displayed'), default=True) 
    enable = models.BooleanField(_('is enable'), default=True) 

class LevelSubLevelZ(models.Model): 
    levelsub = models.ForeignKey(LevelSub, related_name='levelsub_inlevelz') 
    levelz = models.ForeignKey(LevelZ, related_name='levelz_in_levelsub') 
    corder = models.PositiveIntegerField(_('ordering'), default=0) # for ordering 

class LevelCat(models.Model): 
    levelsubs = models.ManyToManyField(LevelSub, verbose_name=_('in levelsubs'), through='LevelCatLevelSub', related_name='inlevelsubs') 
    title = models.CharField(_('yahoo cat name'), max_length=255) 
    is_display = models.BooleanField(_('Is displayed'), default=True) 
    enable = models.BooleanField(_('is enable'), default=True) 

class LevelCatLevelSub(models.Model): 
    levelcat = models.ForeignKey(LevelCat, related_name='levelcat_in_levelsub') 
    levelsub = models.ForeignKey(LevelSub, related_name='levelsub_in_levelcat') 
    corder = models.PositiveIntegerField(_('ordering'), default=0) # for ordering 

class LevelCatItem(models.Model): 
    levelcats = models.ManyToManyField(LevelCat, verbose_name=_('in levelcats'), through='LevelCatItemLevelCat', related_name='inlevelcats') 
    title = models.CharField(_('yahoo catitem name'), max_length=255) 
    is_display = models.BooleanField(_('Is displayed'), default=True) 
    enable = models.BooleanField(_('is enable'), default=True) 

class LevelCatItemLevelCat(models.Model): 
    levelcatitem = models.ForeignKey(LevelCatItem, related_name='levelcatitem_in_levelcat') 
    levelcat = models.ForeignKey(LevelCat, related_name='levelcat_in_levelcatitem') 
    corder = models.PositiveIntegerField(_('ordering'), default=0) # for ordering 

class Product(models.Model): 
    levelcatitems = models.ManyToManyField(LevelCatItem, verbose_name=_('in levelcatitem'), through='ProductLevelCatItem', related_name='inlevelcatitems') 
    title = models.CharField(_('product name'), max_length=255) 
    is_display = models.BooleanField(_('Is displayed'), default=False) 
    update_at = models.DateTimeField(_('Updated at'), auto_now=True) 
    enable = models.BooleanField(_('is enable'), default=True) 

class ProductLevelCatItem(models.Model): 
    levelcatitem = models.ForeignKey(LevelCatItem, related_name='levelcatitem_in_product') 
    product = models.ForeignKey(Product, related_name='product_in_levelcatitem') 
    corder = models.PositiveIntegerField(_('ordering'), default=0) # for ordering 

現在,如果我有:

lz = LevelZ.objects.get(pk=18) 

然後需要列出屬於LZ所有產品,我必須在LevelZ,LevelSub,LevelCat,LevelCatItem表中使用4個manytomany字段。

我TRID使用stubid方式:

lz = LevelZ.objects.get(pk=18) 

lsubs_list = lz.inlevelzs.filter(enable=True).values_list('id', flat=True) 
# lsubs_list = [175L, 171L, 177L, 179L, 181L, 178L, 176L, 180L, 182L] 

lcats_list = LevelCat.objects.filter(levelsubs__in=lsubs_list, enable=True).values_list('id', flat=True) 
# lcats_list = [2123L, 2125L, 2145L, 2113L, 2114L, 2115L, 2116L, 2118L, 2119L, 2117L, 2109L, 2110L] 

lcatitems_list = LevelCatItem.objects.filter(levelcats__in=lcats_list, enable=True).values_list('id', flat=True) 
# lcatitems_list = '[[13013L, 14475L, 14474L, 14480L, 14481L, 14482L, 14483L, 14484L, 14485L, 14486L]' 

products = Product.objects.filter(levelcatitems__in=lcatitems_list, enable=True, is_display=True) 

而且有表現不好....

我知道.prefetch_related()是良好的加入2個表,但我不知道該怎麼加入5個表格。

我該怎麼辦?請幫幫我!!謝謝!!

+0

寫入/繪製您想存檔的結果。 – McAbra

回答

0

這似乎是一個過於複雜的數據庫結構。

但無論您需要加入多少個表,都可以使用雙下劃線語法來查詢連接。

products = Product.objects.filter(levelcatitems__levelcats__levelsubs__levelzs__pk=18) 
+0

謝謝你的回覆! 這可以工作,但是當數據很大時性能下降會導致連接太多的表? 任何方式,非常感謝! –