mirror of
https://github.com/django/django.git
synced 2025-07-05 10:19:20 +00:00
[gsoc2009/admin-ui] Refactored the m2m token input view a bit to make it cleaner, and now properly prepopulating data for the m2m token input widget
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/admin-ui@11928 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
9831258985
commit
911b0b4348
@ -4,9 +4,15 @@
|
|||||||
</a>
|
</a>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
|
$('#id_{{ name }}').val('');
|
||||||
$('#id_{{ name }}').tokenInput("{{ search_path }}", {
|
$('#id_{{ name }}').tokenInput("{{ search_path }}", {
|
||||||
noResultsText: "No results found.",
|
noResultsText: "No results found.",
|
||||||
searchingText: "Searching..."
|
searchingText: "Searching...",
|
||||||
|
prePopulate: [
|
||||||
|
{% for p in prepopulated_data %}
|
||||||
|
{id: {{ p.id }}, name: "{{ p.name }}"},
|
||||||
|
{% endfor %}
|
||||||
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#add_id_{{ name }}').hide();
|
$('#add_id_{{ name }}').hide();
|
||||||
|
@ -289,10 +289,10 @@ class ManyToManySearchInput(ManyToManyRawIdWidget):
|
|||||||
self.search_fields = search_fields
|
self.search_fields = search_fields
|
||||||
super(ManyToManySearchInput, self).__init__(rel, attrs)
|
super(ManyToManySearchInput, self).__init__(rel, attrs)
|
||||||
|
|
||||||
def label_for_value(self, value):
|
def prepopulated_data_for_value(self, value):
|
||||||
key = self.rel.get_related_field().name
|
key = self.rel.get_related_field().name
|
||||||
objs = self.rel.to._default_manager.filter(**{key + '__in': value.split(',')})
|
objs = self.rel.to._default_manager.filter(**{key + '__in': value})
|
||||||
return ','.join([str(o) for o in objs])
|
return [{'id': o.id, 'name': unicode(o)} for o in objs]
|
||||||
|
|
||||||
def get_search_path(self, name):
|
def get_search_path(self, name):
|
||||||
return '../autocomplete/%s/' % name
|
return '../autocomplete/%s/' % name
|
||||||
@ -300,45 +300,41 @@ class ManyToManySearchInput(ManyToManyRawIdWidget):
|
|||||||
def render(self, name, value, attrs=None):
|
def render(self, name, value, attrs=None):
|
||||||
if attrs is None:
|
if attrs is None:
|
||||||
attrs = {}
|
attrs = {}
|
||||||
output = [super(ManyToManySearchInput, self).render(name, value, attrs)]
|
if not attrs.has_key('class'):
|
||||||
if value:
|
attrs['class'] = 'vM2MRawIdAdminField'
|
||||||
value = ','.join([str(v) for v in value])
|
|
||||||
else:
|
if not value:
|
||||||
value = ''
|
value = []
|
||||||
opts = self.rel.to._meta
|
|
||||||
app_label = opts.app_label
|
|
||||||
model_name = opts.object_name.lower()
|
|
||||||
related_url = '../../../%s/%s/' % (app_label, model_name)
|
|
||||||
params = self.url_parameters()
|
params = self.url_parameters()
|
||||||
if params:
|
if params:
|
||||||
url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in params.items()])
|
url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in params.items()])
|
||||||
else:
|
else:
|
||||||
url = ''
|
url = ''
|
||||||
if not attrs.has_key('class'):
|
|
||||||
attrs['class'] = 'vM2MRawIdAdminField'
|
output = [forms.TextInput.render(self, name, ','.join([str(v) for v in value]), attrs)]
|
||||||
# Call the TextInput render method directly to have more control
|
opts = self.rel.to._meta
|
||||||
output = [forms.TextInput.render(self, name, value, attrs)]
|
|
||||||
if value:
|
|
||||||
label = self.label_for_value(value)
|
|
||||||
else:
|
|
||||||
label = u''
|
|
||||||
context = {
|
context = {
|
||||||
'url': url,
|
'url': url,
|
||||||
'related_url': related_url,
|
'related_url': '../../../%s/%s/' % (opts.app_label, opts.object_name.lower()),
|
||||||
'admin_media_prefix': settings.ADMIN_MEDIA_PREFIX,
|
'admin_media_prefix': settings.ADMIN_MEDIA_PREFIX,
|
||||||
'search_path': self.get_search_path(name),
|
'search_path': self.get_search_path(name),
|
||||||
'search_fields': ','.join(self.search_fields),
|
'search_fields': ','.join(self.search_fields),
|
||||||
'model_name': model_name,
|
|
||||||
'app_label': app_label,
|
|
||||||
'label': label,
|
|
||||||
'name': name,
|
'name': name,
|
||||||
|
'model_name': opts.object_name.lower(),
|
||||||
|
'app_label': opts.app_label,
|
||||||
|
'prepopulated_data': self.prepopulated_data_for_value(value),
|
||||||
}
|
}
|
||||||
|
|
||||||
output.append(render_to_string(self.widget_template or (
|
output.append(render_to_string(self.widget_template or (
|
||||||
'templates/widget/%s/%s/m2m_searchinput.html' % (app_label, model_name),
|
'templates/widget/%s/%s/m2m_searchinput.html' % (app_label, model_name),
|
||||||
'templates/widget/%s/m2m_searchinput.html' % app_label,
|
'templates/widget/%s/m2m_searchinput.html' % app_label,
|
||||||
'templates/widget/m2m_searchinput.html',
|
'templates/widget/m2m_searchinput.html',
|
||||||
), context))
|
), context))
|
||||||
|
|
||||||
output.reverse()
|
output.reverse()
|
||||||
|
|
||||||
return mark_safe(u''.join(output))
|
return mark_safe(u''.join(output))
|
||||||
|
|
||||||
class RelatedFieldWidgetWrapper(forms.Widget):
|
class RelatedFieldWidgetWrapper(forms.Widget):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user