2012-10-03 36 views
0

我有自定義插件與下一個結構。 models.py看起來像如何從模板中顯示django-cms自定義插件的數據

class ProductDescription(models.Model): 
     name = models.CharField(max_length=30) 
     icon = models.ImageField(upload_to="installation_image", blank=True, null=True) 
     description = models.TextField(blank=True, null=True) 

     def __unicode__(self): 
      return self.name 

    class ProductPlugin(CMSPlugin): 
      product = models.ForeignKey(ProductDescription) 

    class ProductSpecification(models.Model): 
     product = models.ForeignKey(ProductDescription) 
     specification = models.TextField(blank=True, null=True) 

    class InstallationStep(models.Model): 
     product = models.ForeignKey(ProductDescription) 
     step = models.TextField(blank=True, null=True) 
     image = models.ImageField(upload_to="installation_image", blank=True, null=True) 

admin.py

class InstallationStepInline(admin.StackedInline): 
     model = InstallationStep 
     extra = 0 

    class ProductSpecificationInline(admin.StackedInline): 
     model = ProductSpecification 
     extra = 0 

    class DeviceAdmin(admin.ModelAdmin): 
     inlines = [ProductSpecificationInline , 
     InstallationStepInline] 

    admin.site.register(ProductDescription, DeviceAdmin) 

和cms_plugin.py

class CMSProductPlugin(CMSPluginBase): 
     model = ProductPlugin 
     name = _("Product Description") 
     render_template = "product_description.html" 

     def render(self, context, instance, placeholder): 
      context.update({ 
       'product':instance.product, 
       'object':instance, 
       'placeholder':placeholder 
      }) 

      return context 

    plugin_pool.register_plugin(CMSProductPlugin) 

所以,每個產品可以有沒有或幾種規格和安裝步驟。 而問題是我如何渲染這些規範和模板中的步驟? 我知道如何獲取ProductDescription類的數據。這就像

<div class="test">{{ product.description }}</div> 

但我怎樣才能提取其他數據? !我想這應該是這樣的

{% for steps in product.InstallationStep.all %} 
     <div class="test">{{ steps.step }}</div> 
    {% endfor %} 

,但事先並不做任何事情=(

感謝

回答

1

我想你只需要:

​​

既然你有在InstallationStep模型類中的ForeignKey,django將_set方法放入引用類中。

你可以在django shell中試試這個。如果您嘗試使用foo.bar並且foo中沒有任何欄,模板將自動失敗...

+1

謝謝你!你救了我的一天!只需要注意一點,我必須使用小寫字母,例如product.installationstep_set.all – poul

0

根據the doc,您還需要修改模型中的copy_relations方法。如果不是,發佈草稿時不會複製模型,也不會看到任何內容。我不明白你是如何設法使它工作的。

在你的情況會是這樣,這些:

class ProductPlugin(CMSPlugin): 
    product = models.ForeignKey(ProductDescription) 

    def copy_relations(self, oldinstance): 
     for associated_item in oldinstance.InstallationStep_set.all(): 
      # instance.pk = None; instance.pk.save() is the slightly odd but 
      # standard Django way of copying a saved model instance 
      associated_item.pk = None 
      associated_item.plugin = self 
      associated_item.save() 

注意

不知道有關在該InstallationStep_set,因爲我通常在ForeignKey字段使用related_name參數。例如,

class InstallationStep(models.Model): 
    product = models.ForeignKey(ProductDescription, related_name='installationsteps') 

和你的將是:

for associated_item in oldinstance.installationsteps.all(): 

在任何情況下,感謝您的問題,因爲它成了我的指導。

相關問題