Tutorials for using django-tagging in Python's Django framework

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