D
D
Dmitry Koldyaev2015-08-28 15:48:12
Django
Dmitry Koldyaev, 2015-08-28 15:48:12

Why are inline objects not showing up in django-cms plugins?

Good afternoon. Faced some inappropriate behavior of django-cms.
I describe the model

# models.py

class Email(models.Model):
    email =         models.EmailField(blank=False, null=False, verbose_name=u'Электронная почта')
    order =         models.IntegerField(blank=False, null=False, default=0, verbose_name=u'Сортировка')
    contact_block = models.ForeignKey('contact.ContactBlockPlugin', related_name='emails', blank=False, null=False)
    def __unicode__(self):
        return self.email
    class Meta :
        ordering =  ['order',]

class ContactBlockPlugin(CMSPlugin):
    address =       models.TextField(blank=False, null=False, verbose_name=u'Адрес')

And I register plugins, according to the documentation ( django-cms.readthedocs.org/en/latest/how_to/custom... )
# cms_plugins.py

class EmailInlineAdmin(admin.TabularInline):
    model = Email

class CMSContactBlockPlugin(CMSPluginBase):

    model =     ContactBlockPlugin
    module =    u'Контакты'
    name =      u'Контактный блок'

    render_template =   'contact/contact_block.html'

    inlines =   (EmailInlineAdmin, )

plugin_pool.register_plugin(CMSContactBlockPlugin)

Everything seems to be fine... I, as an authorized user, fill emails from the frontend.
In the template, I calmly iterate over the list of emails
{% if instance.emails.all|length > 0 %}
    <div class="b-contacts mail">
        {% for email in instance.emails.all %}
            <a href="mailto:{{ email.email }}">{{ email.email }}</a>
        {% endfor %}
    </div>
{% endif %}

This is if I'm logged in. But if I try to open this page as a guest (not logged in), the collection instance.emails.allis empty.
I suspect it's the permissions, but I can't figure out where they can be configured for my models.
According to the documentation, I tried to override the render function something like this
def render(self, context, instance, placeholder):
        context = super(CMSContactBlockPlugin, self).render(context, instance, placeholder)
        context.update({
            'emails':   instance.emails.all()
        })
        return context

and iterate like this:
{% for email in emails %}
    <a href="mailto:{{ email.email }}">{{ email.email }}</a>
{% endfor %}

But the result remains the same.
A debajeel is just an empty collection.
What to do, I have no idea.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rooman Kornov, 2015-10-13
@kornov

If you are still interested in this problem, here is an excerpt from the official documentation:
That is, you need to define a method (when defining a model) that copies your inlines to a new instance.
Something like this:

class ContactBlockPlugin(CMSPlugin):
    address = models.TextField(blank=False, null=False, verbose_name=u'Адрес')

    def copy_relations(self, oldinstance):
        for associated_item in oldinstance.mails.all():
            associated_item.pk = None
            associated_item.contact_block = self
            associated_item.save()

Re-read carefully from here - handling relations .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question