def test_keywords_widget(self): """ Test that Keywords widget is returning proper value for form rendering and its support for different data types. """ keyword_widget = KeywordsWidget() keywords = set(["how", "now", "brown"]) Keyword.objects.all().delete() keyword_id_list = [] for keyword in keywords: keyword_id = Keyword.objects.get_or_create(title=keyword)[0].id keyword_id_list.append(keyword_id) keyword_id_string = ",".join(map(str, keyword_id_list)) values_from_string = keyword_widget.decompress(keyword_id_string) self.assertIn("how", values_from_string[1]) self.assertIn("now", values_from_string[1]) self.assertIn("brown", values_from_string[1]) for keyword_id in keyword_id_list: AssignedKeyword.objects.create(keyword_id=keyword_id, content_object=RichTextPage(pk=1)) assigned_keywords = AssignedKeyword.objects.all() values_from_relation = keyword_widget.decompress(assigned_keywords) self.assertIn("how", values_from_relation[1]) self.assertIn("now", values_from_relation[1]) self.assertIn("brown", values_from_relation[1]) self.assertEqual(("", ""), keyword_widget.decompress(None))
def formfield(self, **kwargs): """ Provide the custom form widget for the admin, since there isn't a form field mapped to ``GenericRelation`` model fields. """ from mezzanine.generic.forms import KeywordsWidget kwargs["widget"] = KeywordsWidget() return super(KeywordsField, self).formfield(**kwargs)