/ / Django-cms / Django SingleRelatedObjectDescriptor anstelle des Objekts selbst - Django, Django-Modelle, Django-cms

Django-cms / Django SingleRelatedObjectDescriptor anstelle des Objekts selbst - django, django-models, django-cms

Ich versuche ein Plugin für django-cms zu erstellen und habe Probleme, meine Konfiguration an das zu übergeben CMSPluginBase Klasse.

Das ist meine 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")

Ich habe ein Problem beim Zugriff auf die Objektreferenz von root_shown

Ich versuche so etwas;

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)

Ich möchte das eigentliche Objekt mit abrufen context["Sobj"] = self.model.sectionconfig Stattdessen bekomme ich etwas, das sich auf das Objekt zu beziehen scheint, aber nicht auf das Objekt selbst.

Dies ist, was meine Seite anzeigt;

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

Wie greifen Sie direkt auf das Objekt zu?

Antworten:

1 für die Antwort № 1

In Ihrem Plugin sollten Sie über das auf Objektfelder zugreifen instance Argument zu rendernicht self.model. So was:

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)