This article mainly introduces the use of django-tagging in the Python Django Framework, for the function of the tag part of network programming to help, the need for friends can refer to the
Django uses the app mechanism to reuse components, and making full use of existing apps can greatly simplify the development effort. Currently Django's app is not rich enough, but there are still some good ones. Django-tagging is a good app.
Now the application of tag is very extensive, tag basically became one of the essential items of each website, Django-tagging is an app that provides the tag function. Django-tagging provides a very rich and easy to use. Below I will introduce some commonly used usage, let everybody to this app have a basic understanding, more detailed introduction or honest to see the use of django-tagging:).
Tagging.fields.TagField
We first define a database model widget, and the following examples are illustrated with widgets
?
1 2 3 |
Class Widget (models. Model): name = models. Charfield (max_length=50) tags = Tagfield () |
As the above code, as long as the database model to add Tags field can provide tag support for the object. tags are mapped to Charfield, when adding tag for an object, a comma-delimited string such as:
?
1 |
Widget (name= ' Hello ', tags= ' Test,hi,hello ') |
This adds the test Hi Hello three tag to the newly created object.
Get the code for all objects under a tag as follows:
?
1 2 3 |
#取出所有属于TAG Hi Object tag = get_object_or_404 (tag, name= ' Hi ') widgets = TaggedItem.objects.get_by_model (Widget, tag) |
To remove all the tag code used by the widget is:
?
1 |
tags = Widget.tags.all () |