/ / Django-cms/Django SingleRelatedObjectDescriptor zamiast samego obiektu - django, django-models, django-cms

Django-cms / Django SingleRelatedObjectDescriptor zamiast samego obiektu - django, django-models, django-cms

Próbuję zrobić wtyczkę dla django-cms i mam problemy z przekazaniem mojej konfiguracji do CMSPluginBase klasa.

To jest mój 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")

Mam problem z dostępem do odwołania do obiektu przez root_shown

Próbuję czegoś takiego;

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)

Chcę odzyskać rzeczywisty obiekt za pomocą context["Sobj"] = self.model.sectionconfig ale zamiast tego otrzymuję coś, co wydaje się być odniesieniem do przedmiotu, ale nie do samego przedmiotu.

Tak wyświetla się moja strona;

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

Jak uzyskać bezpośredni dostęp do obiektu?

Odpowiedzi:

1 dla odpowiedzi № 1

We wtyczce powinieneś uzyskać dostęp do pól obiektów za pomocą instance argument do render, nie self.model. Lubię to:

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)