Merge pull request #17 from stefanfoulis/feature/structured-data
adds extra_data support for AutoModelSelect2Field
This commit is contained in:
commit
1e6689eac2
|
@ -158,6 +158,19 @@ class ModelResultJsonMixin(object):
|
|||
"""
|
||||
return smart_unicode(obj)
|
||||
|
||||
def extra_data_from_instance(self, obj):
|
||||
"""
|
||||
Sub-classes should override this to generate extra data for values. These are passed to
|
||||
Javascript and can be used for custom rendering.
|
||||
|
||||
:param obj: The model object.
|
||||
:type obj: :py:class:`django.model.Model`
|
||||
|
||||
:return: The extra data dictionary.
|
||||
:rtype: :py:obj:`dict`
|
||||
"""
|
||||
return {}
|
||||
|
||||
def prepare_qs_params(self, request, search_term, search_fields):
|
||||
"""
|
||||
Prepares queryset parameter to use for searching.
|
||||
|
@ -246,7 +259,8 @@ class ModelResultJsonMixin(object):
|
|||
res = list(qs.filter(*params['or'], **params['and']))
|
||||
has_more = False
|
||||
|
||||
res = [(getattr(obj, self.to_field_name), self.label_from_instance(obj), ) for obj in res]
|
||||
res = [(getattr(obj, self.to_field_name), self.label_from_instance(obj), self.extra_data_from_instance(obj))
|
||||
for obj in res]
|
||||
return (NO_ERR_RESP, has_more, res, )
|
||||
|
||||
|
||||
|
|
|
@ -98,8 +98,13 @@ class Select2View(JSONResponseMixin, View):
|
|||
err, has_more, results = output
|
||||
res = []
|
||||
if err == NO_ERR_RESP:
|
||||
for id_, text in results:
|
||||
res.append({'id': id_, 'text': text})
|
||||
for result in results:
|
||||
id_, text = result[:2]
|
||||
if len(result)>2:
|
||||
extra_data = result[2]
|
||||
else:
|
||||
extra_data = {}
|
||||
res.append(dict(id=id_, text=text, **extra_data))
|
||||
return {
|
||||
'err': err,
|
||||
'more': has_more,
|
||||
|
|
Loading…
Reference in New Issue