嗨,我正在尝试编写一个django webapp,它位于一个遗留数据库之上,因此我不能控制太多的模型字段,也不能使用新模型实现功能。在
我有许多文档带有用户指定的逗号分隔标记。我想根据共享标记对“相关”文档进行分组。在
# Model to express legacy table
class Document(models.Model):
id = models.BigIntegerField(primary_key= True)
metadata_id = models.CharField(max_length=384)
tags_as_csv = models.TextField()
# Created new Model tag_text extracted from tags_as_csv
class Tagdb(models.Model):
tagid = models.BigIntegerField(primary_key=True)
referencing_document = models.ForeignKey(Document)
tag_text = models.TextField(blank=True)
因此,一份文件将包含:
^{pr2}$Tagdb将有如下条目
id , referencing_document , tag_text
1 , 1 , "raw-data"
2 , 1 , "high temperature"
3 , 1 , "important"
4 , 2 , "important"
5 , 2 , "processed-data"
6 , 3 , "important"
7 , 4 , "processed-data"
现在我要提取与父文档对应的标记匹配的所有文档对象。我使用下面的get_queryset方法来完成。在
def get_queryset(self, **kwargs):
parent_document = Document.objects.get(id=self.kwargs['slug'])
tags_in_parent_document = [x.tag_text for x in Tagdb.objects.filter(referencing_document=parent_document.id)]
# This will contain all the Document ids that match all the tags
queryset_with_duplicates = []
for tag in tags_in_parent_document:
queryset_with_duplicates.extend([x.referencing_document.id for x in Tagdb.objects.filter(tagtext__icontains=tag)])
# Make sure we have only unique ids
queryset_unique = set(queryset_with_duplicates)
# Get all the Document objects
queryset = Document.objects.filter(id__in=queryset_unique)
return queryset
我的问题是:有没有更好的方法。我能以某种方式获取包含父文档中所有标记的所有文档并过滤掉重复的文档(因为多个文档包含同一个标记)。在
最好创建两个额外的模型:一个用于标记,另一个用于标记和文档之间的链接。如果这是不可接受的,你可以使用类似的方法:
另外,添加用于获取/设置标记的模型方法,它将简化任何可能的重构。在
相关问题 更多 >
编程相关推荐