简体   繁体   English

使用django-taggit,是否可以将标签限制为预先批准的值?

[英]Using django-taggit, is it possible to limit the tags to pre approved values?

I have a question about the use of django-taggit. 我对django-taggit的使用有疑问。 I have a UserProfile (which I attach using AUTH_PROFILE_MODULE in the settings file) in which I store a set of skills for tutors such as: , , etc. Then, when someone wants to request a tutoring session, they can write a description of what they want and place tags for their request. 我有一个UserProfile(我在设置文件中使用AUTH_PROFILE_MODULE附加),其中我为导师存储了一组技能,例如:,等等。然后,当有人想要请求辅导会话时,他们可以写一个什么的描述他们想要并为他们的请求放置标签。 (For example, I want a tutor skilled in calculus and physics). (例如,我想要一个精通微积分和物理学的导师)。 If I let the users of the site choose their own tags, then I worry that we could end up with "tag hell" where we have tags such as , , etc. So, I want to tag skills, but only from a table that I populate in the admin as we add people. 如果我让网站的用户选择他们自己的标签,那么我担心我们最终会遇到“标签地狱”,我们有标签,如,等等。所以,我想标记技能,但只能从表格中当我们添加人员时,我会填充管理员。 That avoids the diffusion problem (similar to how stackoverflow works). 这避免了扩散问题(类似于stackoverflow的工作方式)。

Here is some trial Code: 这是一些试用代码:

from django.db import models </br>
from django.contrib.auth.models import User
from taggit.managers import TaggableManager

class BaseUser(models.Model):
    class Meta:
        abstract=True
    first_name=models.CharField(max_length=100)
    skills=TaggableManager()

class UserProfile(BaseUser):
    user=models.ForeignKey(User,unique=True)

class TutoringSession(models.Model):
    title=models.CharField(max_length=100,blank=False)
    slug=models.SlugField(max_length=250,unique=True,blank=False,editable=False)
    tags=TaggableManager()

Or, is it better to use a Tags class: 或者,使用Tags类更好:

class Tags:
     name=models.CharField(max_length=100, blank=False, unique=True)

and set up a ManyToMany relation to it in both TutoringSession and UserProfile? 并在TutoringSession和UserProfile中设置与它的ManyToMany关系?

Thanks! 谢谢!

I should note that this is related to question: What benefit does Django-Taggit provide over a simple ManyToManyField() implementation of tagging? 我应该注意到这与问题有关: Django-Taggit对标记的简单ManyToManyField()实现有什么好处?

except that in that example, we might want to limit the set of allowed answers to red and purple (that we've defined in a table because it might change) 除了在那个例子中,我们可能想要将允许的答案限制为红色和紫色(我们在表中定义,因为它可能会改变)

So you want a set of predefined tags and the Users as well as the TutoringSessions should relate to one or more of these tags. 因此,您需要一组预定义标记,用户以及TutoringSessions应与这些标记中的一个或多个相关。 Thats (as far as I can see) what m2m-fields are made for. 多数民众赞成(据我所知)m2m-fields的用途。 Maybe taggit has some usability advantages (I'm not familiar with it), but the functionality described here can be achieved with simple m2m-fields. 也许taggit有一些可用性优势(我不熟悉它),但这里描述的功能可以用简单的m2m字段来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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