Skip to content

Instantly share code, notes, and snippets.

@timonweb
Forked from unexceptable/inline_panel.js
Created August 21, 2018 10:52
Show Gist options
  • Save timonweb/a23f68463bb7b94aa9966ff4f93051a8 to your computer and use it in GitHub Desktop.
Save timonweb/a23f68463bb7b94aa9966ff4f93051a8 to your computer and use it in GitHub Desktop.
OneToOne Wagtail InlinePanel
// stuff/templates/wagtailadmin/edit_handlers/inline_panel.js
(function() {
var opts = {
formsetPrefix: "id_{{ self.formset.prefix }}",
emptyChildFormPrefix: "{{ self.empty_child.form.prefix }}",
canOrder: {% if can_order %}true{% else %}false{% endif %},
maxForms: {{ self.formset.max_num }}
};
var panel = InlinePanel(opts);
{% for child in self.children %}
panel.initChildControls("{{ child.form.prefix }}");
{% endfor %}
panel.updateAddButtonState = function() {
if (opts.maxForms) {
var forms = panel.formsUl.children('li:visible');
var addButton = $('#' + opts.formsetPrefix + '-ADD');
if (forms.length >= opts.maxForms) {
addButton.hide();
} else {
addButton.show();
}
}
};
panel.setHasContent();
panel.updateMoveButtonDisabledStates();
panel.updateAddButtonState();
})();
# stuff/models.py
class SomeDocument(models.Model):
document = models.ForeignKey(
'wagtaildocs.Document',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
page = ParentalKey(
'stuff.SomePage',
related_name='my_one_document',
unique=True
)
class SomePage(Page):
body = RichTextField(blank=True)
content_panels = [
FieldPanel('body'),
InlinePanel('my_one_document', label="Document"),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment