Tutorial on using Django-tagging in Python django framework

Source: Internet
Author: User
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()

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.