0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

implement SnippetChooserBlock

This commit is contained in:
Matt Westcott 2015-02-11 12:39:44 +00:00
parent dfb2a999f4
commit 85902d111d
2 changed files with 20 additions and 3 deletions

View File

@ -0,0 +1,17 @@
from __future__ import unicode_literals
from django.utils.functional import cached_property
from django.contrib.contenttypes.models import ContentType
from wagtail.wagtailadmin.blocks import ChooserBlock
class SnippetChooserBlock(ChooserBlock):
def __init__(self, target_model, **kwargs):
super(SnippetChooserBlock, self).__init__(**kwargs)
self.target_model = target_model
@cached_property
def widget(self):
from wagtail.wagtailsnippets.widgets import AdminSnippetChooser
content_type = ContentType.objects.get_for_model(self.target_model)
return AdminSnippetChooser(content_type)

View File

@ -22,10 +22,10 @@ class AdminSnippetChooser(AdminChooser):
self.target_content_type = content_type
def render_html(self, name, value, attrs):
original_field_html = super(AdminSnippetChooser, self).render_html(name, value, attrs)
model_class = self.target_content_type.model_class()
instance = self.get_instance(model_class, value)
instance, value = self.get_instance_and_id(model_class, value)
original_field_html = super(AdminSnippetChooser, self).render_html(name, value, attrs)
return render_to_string("wagtailsnippets/widgets/snippet_chooser.html", {
'widget': self,