Tag fields in ModelAdmin
To support TagField and SingleTagField fields in the admin, you need to register the Model and ModelAdmin using Tagulous's
register() function, instead of the standard one:
import tagulous class MyAdmin(admin.ModelAdmin): list_display = ['name', 'tags'] tagulous.admin.register(MyModel, MyAdmin)
This will make a few changes to
MyAdmin to add tag field support (detailed below), and then register it with the default admin site using the standard
As with the normal registration call, the admin class is optional:
You can also pass a custom admin site into the `register()` function:
# These two lines are equivalent: tagulous.admin.register(myModel, MyAdmin) tagulous.admin.register(myModel, MyAdmin, site=admin.site)
The changes Tagulous's
register() function makes to the
- Changes your
list_displayfor any tag fields, and adds functions to the
ModelAdminto display the tag string (unless an attribute with that name already exists)
- Switches an inline class to a
- You can only provide the Tagulous
register()function with one model.
- The admin class will be modified; bear that in mind if registering it with multiple admin sites. In that case, you may want to enhance the class manually, as described below.
Manually enhancing your ModelAdmin
tagulous.admin.register function is the short way to enhance your admin classes. If for some reason you can't use it (eg another library which has its own
register function, or you're registering it with more than one admin site), you can do what it does manually:
Change your admin class to subclass
This disables Django's green button to add a related field, which is incompatible with Tagulous.
This finds the tag fields on the model class, and adds support for them to
- Register the admin class as normal
import tagulous class MyAdmin(tagulous.admin.TaggedModelAdmin): list_display = ['name', 'tags'] tagulous.admin.enhance(MyModel, MyAdmin) admin.site.register(MyModel, MyAdmin)
The admin site can use different autocomplete settings to the public site by changing the settings
TAGULOUS_ADMIN_AUTOCOMPLETE_CSS. You may want to do this to avoid jQuery being loaded more than once, for example - assuming the version in Django's admin site is compatible with the autocomplete library of your choice.
See Settings for more information.
Managing the tag model
Tagulous provides additional tag-related functionality for tag models, such as the ability to merge tags. You can use Tagulous's
register function to do this for you - just pass it the tag field:
You can also specify the tag model directly:
If you have a custom tag model and want to extend the admin class for extra fields on your custom model, you can subclass the
TagModelAdmin class to get the extra tag management functionality:
class MyModelTagsAdmin(tagulous.admin.TagModelAdmin): list_display = ['name', 'count', 'protected', 'my_extra_field'] admin.site.register(MyCustomTagModel, MyModelTagsAdmin)
When overriding options, you should base them on the options in the default
list_display = ['name', 'count', 'protected'] list_filter = ['protected'] exclude = ['count'] actions = ['merge_tags']
TagTreeModelAdmin also excludes the
Remember that the relationship between your entries and tags are standard
ManyToMany relationships, so deletion propagation will work as it would normally.