Admin
Tag fields in ModelAdmin
Tagulous automatically enhances Django’s admin so that tag fields work correctly when you register models using the standard Django admin API:
from django.contrib import admin
from myapp.models import MyModel
@admin.register(MyModel)
class MyAdmin(admin.ModelAdmin):
list_display = ['name', 'tags']
Autocomplete settings
The admin site can use different autocomplete settings to the public site by
changing the settings TAGULOUS_ADMIN_AUTOCOMPLETE_JS and
TAGULOUS_ADMIN_AUTOCOMPLETE_CSS. You may want to do this to change the autocomplete
library.
See Settings for more information.
Because the select2 control defaults to use the same width as the form element it
replaces, you may find this a bit too small in some versions of the Django admin. You
could override this with autocomplete_settings on the field, but that
will change non-admin controls too - instead, set TAGULOUS_ADMIN_AUTOCOMPLETE_SETTINGS
to apply it to the admin only:
TAGULOUS_ADMIN_AUTOCOMPLETE_SETTINGS = {"width": "75%"}
Alternatively, add a custom stylesheet to TAGULOUS_ADMIN_AUTOCOMPLETE_CSS with a
rule such as:
.select2 {
width: 75% !important;
}
Managing the tag model
Tagulous provides additional tag-related functionality for tag models, such as the ability to merge tags. Auto-enhancement applies this automatically when you register a tag model with the standard admin:
admin.site.register(MyModel.tags.tag_model)
You can also pass the tag field descriptor or the tag model class directly:
admin.site.register(MyModel.tags.tag_model)
admin.site.register(MyCustomTagModel)
If you have a custom tag model, subclass TagModelAdmin:
from django_tagulous.admin import TagModelAdmin
class MyModelTagsAdmin(TagModelAdmin):
list_display = ['name', 'count', 'protected', 'my_extra_field']
admin.site.register(MyCustomTagModel, MyModelTagsAdmin)
When overriding options, you should base them on the defaults in
TagModelAdmin:
list_display = ['name', 'count', 'protected']
list_filter = ['protected']
search_fields = ['name']
exclude = ['count']
actions = ['merge_tags']
prepopulated_fields = {'slug': ('name',)}
The TagTreeModelAdmin also excludes the parent, path, label and
level fields.
Remember that the relationship between your entries and tags are standard
ForeignKey or ManyToMany relationships, so deletion propagation will
work as it would normally.
Disabling auto-enhancement
You can set TAGULOUS_ENHANCE = False in your settings to opt out of the
global AdminSite.register patch. You can then apply tag field support
selectively by subclassing TaggedModelAdmin directly:
from django_tagulous.admin import TaggedModelAdmin
@admin.register(MyModel)
class MyAdmin(TaggedModelAdmin):
list_display = ['name', 'tags']