Django-cms / Django SingleRelatedObjectDescriptor而不是对象本身

时间:2015-05-29 18:27:12

标签: django django-models django-cms

我正在尝试为django-cms创建一个插件,并且在将我的配置传递给CMSPluginBase类时遇到问题。

这是我的models.py;

from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
from cms.models.pluginmodel import CMSPlugin

class Section(MPTTModel):
    name = models.CharField(max_length=25, unique=True)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)

    class MPTTMeta:
        order_insertion_by = ['name']

    def __str__(self):
        return self.name


class SectionConfig(CMSPlugin):
    title = models.CharField(default="Usefull Links", max_length=25)
    root_shown = models.ForeignKey('Section')

我遇到了root_shown

访问对象引用的问题

我正在尝试这样的事情;

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from cms.models.pluginmodel import CMSPlugin
from django.utils.translation import ugettext_lazy as _
from links_plugin.models import Section, SectionConfig

class LinksPlugin(CMSPluginBase):
    name = _("Links Tree Plugin")
    model = SectionConfig
    render_template = "links.html"
    cache = False

    def render(self, context, instance, placeholder):
        context['instance'] = instance
        context['Sobj'] = self.model.sectionconfig
        return context

plugin_pool.register_plugin(LinksPlugin)

我想用context['Sobj'] = self.model.sectionconfig检索实际对象,但我得到的东西似乎是对象的引用,而不是对象本身。

这是我的页面显示的内容;

django.db.models.fields.related.SingleRelatedObjectDescriptor object at 0x3acb790

如何直接访问该对象?

1 个答案:

答案 0 :(得分:1)

在插件中,您应该通过render参数self.model访问对象字段,而不是from cms.plugin_base import CMSPluginBase from cms.plugin_pool import plugin_pool from cms.models.pluginmodel import CMSPlugin from django.utils.translation import ugettext_lazy as _ from links_plugin.models import Section, SectionConfig class LinksPlugin(CMSPluginBase): name = _("Links Tree Plugin") model = SectionConfig render_template = "links.html" cache = False def render(self, context, instance, placeholder): context['instance'] = instance context['Sobj'] = instance.sectionconfig return context plugin_pool.register_plugin(LinksPlugin) 。像这样:

number1 = NumberFormat(20.5/80 * 100,'9.999')