Django uses the app mechanism to implement component reuse, and the full use of existing apps can greatly simplify the development effort. The current Django app is not rich enough, but there are still some good ones. Django-tagging is a good app.
Tag is now widely used, tag is basically one of the necessary items of each website, django-tagging is a tag function to provide the app. Django-tagging offers a very rich range of features that are very simple to use. Below I introduce some common usage, let everybody have a basic understanding of the app, more detailed introduction or honestly to see the use of django-tagging:).
Tagging.fields.TagField
We first define a database model widget, and the following examples are all illustrated with widgets.
Class Widget (models. Model): name = models. Charfield (max_length=50) tags = Tagfield ()
As in the code above, you can provide tag support for an object as long as you add the tags field to the database model. tags are mapped to Charfield, which is a comma-delimited string when adding tags to an object, such as:
Widgets (name= ' Hello ', tags= ' Test,hi,hello ')
This adds the test Hi Hello three tag to the newly created object.
The code for getting all the objects under a tag is as follows:
#取出所有属于TAG Hi Object tag = get_object_or_404 (tag, name= ' Hi ') widgets = TaggedItem.objects.get_by_model (Widget, Tag
To remove all tag tags used by the widget:
tags = Widget.tags.all ()