Django-cms/Django SingleRelatedObjectDescriptor 而不是对象本身
Django-cms/Django SingleRelatedObjectDescriptor instead of object itself
我正在尝试为 django-cms 制作一个插件,但在将我的配置传递给 CMSPluginBase
class.
时遇到问题
这是我的 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
如何直接访问对象?
在您的插件中,您应该通过 render
的 instance
参数访问对象字段,而不是 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)
我正在尝试为 django-cms 制作一个插件,但在将我的配置传递给 CMSPluginBase
class.
这是我的 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
如何直接访问对象?
在您的插件中,您应该通过 render
的 instance
参数访问对象字段,而不是 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)