简体   繁体   中英

django: django-taggit usage

This is probably a stupid question but I downloaded Django-taggit and the docs mentioned adding a TaggableManager manager to the each model I want to associate. This is fine, but what about models from external apps I "pip-ed"? What's the best way to use taggit with these models?

You could subclass the model in the external app in one of your own apps, that would probably be a reasonable solution, ie

from someapp.models import SomeModel
from taggit.managers import TaggableManager

class SomeModelTagged(SomeModel):
    tags = TaggableManager()

Then in the views where you used SomeModel from the external app you would have to use your new model instead.

You can easily register model from any of your external apps with taggit. Assume the name of the model is Item.

from taggit.managers import TaggableManager
from external_app.models import Item

Item.add_to_class('tags', TaggableManager())

And then you can use taggit in usual way.

i = Item.objects.get(pk=1)
i.tags.add("wassup")
i.tags.all()

您可以使用(-e VCS + REPOS_URL [@REV]#egg = PACKAGE)安装可编辑版本并自行添加django-taggable集成。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM