Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django taggit防止不同模型之间的标记重叠_Python_Django Models_Django Taggit - Fatal编程技术网

Python django taggit防止不同模型之间的标记重叠

Python django taggit防止不同模型之间的标记重叠,python,django-models,django-taggit,Python,Django Models,Django Taggit,我有两种不同的型号 class MessageArchive(models.Model): from_user = models.CharField(null=True, blank=True, max_length=300) archived_time = models.DateTimeField(auto_now_add=True) label = models.ForeignKey(MessageLabel, null=True, blank=True) a

我有两种不同的型号

class MessageArchive(models.Model):
    from_user = models.CharField(null=True, blank=True, max_length=300)
    archived_time = models.DateTimeField(auto_now_add=True)
    label = models.ForeignKey(MessageLabel, null=True, blank=True)
    archived_by = models.ForeignKey(OrgStaff)
    tags = TaggableManager()
现在,我已经为消息定义了
垃圾邮件
待办事项
紧急
标记

然后我有另一个模型:

class PersonArchive(models.Model):
        from_user = models.CharField(null=True, blank=True, max_length=300)
        archived_time = models.DateTimeField(auto_now_add=True)
        label = models.ForeignKey(MessageLabel, null=True, blank=True)
        archived_by = models.ForeignKey(OrgStaff)
        tags = TaggableManager()
我为模特儿定义了
awesome
legend
rockstar
。可能没有更多的定义

很明显,我不希望person和message的标签重叠。 我应该如何做到这一点?谢谢

您可以在ForeignKeyFields和ManyToManyFields上使用该功能。您的models.py文件可能如下所示:

class PersonArchive(models.Model):
    tags_field = models.ManyToManyField(Tag, related_name="people_archives", limit_choices_to={'message_archives__isnull': True})

class MessageArchive(models.Model):
    tags_field = models.ManyToManyField(Tag, related_name="message_archives", limit_choices_to={'people_archives__isnull': True})

就我所知,您需要两种不同型号的标签的不同基本族。 考虑到我不是taggit方面的专家,所以我提出的解决方案可能有点过于复杂,但通过查看源代码,这是第一个让我想到的解决方案。 您可以通过扩展TaggableManager使用的TaggableRel类并向limit_choices_to参数添加一个条件来实现这一点:

扩展TaggableRel

class CustomTaggableRel(TaggableRel):
    def __init__(self, field, model_name):
        super(TaggableRel, self ).__init__(field)
        self.limit_choices_to = {'content_type': model_name}
然后通过以下方式扩展TaggableManager:

class CustomTaggableManager(TaggableManager):
    def __init__(self, model_name=None, verbose_name=_("Tags"),
        help_text=_("A comma-separated list of tags."), through=None, blank=False):
        super(TaggableManager, self ).__init__(verbose_name, help_text, through, blank)
        self.rel = CustomTaggableRel(self, model_name)
比你的模特更重要的是:

class PersonArchive(models.Model):
        .
        .
        .
        tags = CustomTaggableManager(model_name="PersonArchive")

这应该可以解决问题,我没有尝试解决方案,而且我写得很快,但这可能会让你走上正确的道路。

我自己就解决了。我决定让我的标签混合在一起,因为我找到了一种只过滤特定型号标签的方法。这将仅过滤
modelname
的标记。您可以根据需要展开过滤器

Tag.objects.filter(taggit_taggeditem_items__content_type__model='modelname')

使用
标记\u字段
标记管理器
有什么区别??我的意思是,他们两个会如何相处?