Answer the question
In order to leave comments, you need to log in
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'Адрес')
# 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)
{% 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 %}
instance.emails.all
is empty. def render(self, context, instance, placeholder):
context = super(CMSContactBlockPlugin, self).render(context, instance, placeholder)
context.update({
'emails': instance.emails.all()
})
return context
{% for email in emails %}
<a href="mailto:{{ email.email }}">{{ email.email }}</a>
{% endfor %}
Answer the question
In order to leave comments, you need to log in
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()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question