/ / Django-cms / Django SingleRelatedObjectDescriptor замість самого об'єкта - django, django-models, django-cms

Django-cms / Django SingleRelatedObjectDescriptor замість самого об'єкта - 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 для відповіді № 1

У плагіні ви повинні отримати доступ до полів об'єкта через instance аргумент на 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)