-
-
Save timonweb/a23f68463bb7b94aa9966ff4f93051a8 to your computer and use it in GitHub Desktop.
OneToOne Wagtail InlinePanel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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