A tutorial on using django-tagging under the Python Django framework

Source: Internet
Author: User

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 ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.