4👍
I made a really dirty try to solve my issue. It works and I want to share it with you.
For better understanding I use new Models in this example:
class Tag(models.Model):
name = models.CharField(max_length=100, verbose_name="Name")
class Book(models.Model):
tags = models.ManyToManyField(Tag)
read = models.BooleanField()
class BookAdmin(ModelAdmin):
list_filter = ['read', ]
admin.site.register(Book, BookAdmin)
At first, I overwrote the changelist_view
of the BookAdmin.
def changelist_view(self, request, extra_context=None):
extra_context = extra_context if extra_context else {}
q = request.GET.copy()
tags = Tag.objects.all().values('id', 'name')
current_tags = q.get('tags__id__in', [])
tag_query = request.GET.copy()
if current_tags:
tag_query.pop('tags__id__in')
current_tags = current_tags.split(',')
all_tag = False
else:
all_tag = True
for tag in tags:
if str(tag['id']) in current_tags:
tag['selected'] = True
temp_list = list(current_tags)
temp_list.remove(str(tag['id']))
tag['tag_ids'] = ','.join(temp_list)
else:
tag['selected'] = False
tag['tag_ids'] = ','.join(current_tags)
extra_context['tag_query'] = '?' if len(tag_query.urlencode()) == 0 else '?' + tag_query.urlencode() + '&'
extra_context['all_tag'] = all_tag
extra_context['tags'] = tags
return super(BookAdmin, self).changelist_view(request, extra_context=extra_context)
As you can see, I look in GET, whether there some Tags are chosen or not. Then I build new GET-Parameter for each possible Tag.
And then there is my overwriten change_list.html
{% extends "admin/change_list.html" %}
{% block content %}
{{ block.super }}
<h3 id="custom_tag_h3"> Fancy Tag filter</h3>
<ul id="custom_tag_ul">
<li{% if all_tag %} class="selected"{% endif %}>
<a href="{{ tag_query }}">All</a>
</li>
{% for tag in tags %}
<li{% if tag.selected %} class="selected"{% endif %}>
<a href="{{ tag_query }}tags__id__in={{ tag.tag_ids }}{% if not tag.selected %}{% if tag.tag_ids %},{% endif %}{{ tag.id }}{% endif %}">{{ tag.name }}</a>
</li>
{% endfor %}
</ul>
<script type="text/javascript">
$('#changelist-filter').append($('#custom_tag_h3'));
$('#custom_tag_h3').after($('#custom_tag_ul'));
</script>
{% endblock content %}
This way I have a filter looking like the boolean read
-filter where I can activate more than one option. By clicking on the filter, a new id is added to the query. Another click on already selected option removes id from query. Click on All removes the hole tags_in
-parameter from URL.
0👍
Another option to allow users to use an autocomplete in the changelist view:
-
setup
ModelAdmin.search_fields
to search in the fields served by the Autocomplete, ie.class ModelBAdmin(ModelAdmin): search_fields = ['model_a_relation__name']
-
override the changelist tomplate to spawn an autocomplete on the search input:
<script type="text/javascript"> $(document).ready(function() { $('#searchbar').yourlabsAutocomplete({ url: '{% url 'autocomplete_light_autocomplete' 'YourAutocompleteName' %}', choiceSelector: '[data-value]', }).input.bind('selectChoice', function(e, choice, autocomplete) { $(this).val(choice.text()) .parents('form').submit(); }); }); </script>
-
ensure that there is no JS error after adding this script !
- [Django]-Plugin with editable link element
- [Django]-Django Postgresql syncdb error
- [Django]-How to populate a model field via django signals with post_save?