This article describes how to use Django-tagging in the Python django framework, and provides help for some of the tag functions in network programming, you can refer to django's app mechanism to reuse components. making full use of existing apps can greatly simplify development. At present, although the app in django is not rich enough, there are still some good ones. Django-tagging is a good app.
Currently, tags are widely used. tags are basically one of the essential items for various websites. django-tagging is an app that provides the tag function. Django-tagging provides a wide range of functions, but is easy to use. Next I will introduce some common usage, so that you can have a basic understanding of the app. For more details, let's look at the django-tagging instructions in a down-to-earth manner :).
Tagging. fields. TagField
Let's first define a database model Widget. the following examples use widgets to describe it.
class Widget(models.Model): name = models.CharField(max_length=50) tags = TagField()
As for the above code, you only need to add the tags field in the database model to provide tag support for this object. Tags is mapped to CharField. when a tag is added to an object, strings separated by commas (,) are as follows:
Widget(name='hello', tags='test,hi,hello')
In this way, three tags test hi hello are added to the newly created object.
The code for getting all objects under a tag is as follows:
# Retrieve all TAG hi object tags = get_object_or_404 (tag, name = 'hi') widgets = TaggedItem. objects. get_by_model (Widget, Tag)
The code for retrieving all tags used by the Widget is:
tags = Widget.tags.all()