gpt4 book ai didi

jquery - Django "Similar Objects"基于相似标签集

转载 作者:行者123 更新时间:2023-11-29 13:24:04 26 4
gpt4 key购买 nike

假设您有 1k 个对象,每个对象都有 10-50 个标签。

对于任何给定的对象,必须有一个对象列表,这些对象具有与其相同的关键字,从多到少。

然而,在 Django 参数中,我不知道如何以这种方式进行查询。

django 中是否存在这样的东西,或者我应该在 django 的约束之外编写算法吗?


class Product(models.Model):

product_id = models.IntegerField(
unique=True,
)

slug = models.SlugField(
unique=True,
blank = True,
null = True,
)

meta_description = models.TextField(
max_length = 160,
blank = True,
null = True,
)


title = models.CharField(
max_length = 160,
blank = True,
null = True,
)


description = models.TextField(
blank = True,
null = True,
)

first_subject_heading = models.CharField(
max_length = 160,
blank = True,
null = True,
)

description_main = models.TextField(
blank = True,
null = True,
)

price = models.DecimalField(
max_digits=6,
decimal_places=2,
blank = True,
null = True,
)

published = models.DateTimeField(auto_now_add=True, blank=True)

tags = TaggableManager(
blank = True,
)

category = models.ManyToManyField(
'ProductCategory',
blank = True,
)

license_selection_model = models.IntegerField(
default = 1,
blank = True,
null = True,
)

minipic = models.ImageField(
upload_to='minipics/',
blank = True,
null = True,
)

def get_absolute_url(self):
from django.core.urlresolvers import reverse
return reverse('store.views.product', args=[self.slug])

def save(self, *args, **kwargs):
if not self.id and not self.slug:
#Only set the slug when the object is created.
self.slug = slugify(self.title) #Or whatever you want the slug to use
super(Product, self).save(*args, **kwargs)

def __str__(self):
return "%i, %s"%(self.product_id, self.title)

这是使用 postgres 和 taggit 扩展(taggit 的“similars”选项有错误)

最佳答案

Django Taggit提供一个 TaggableManager帮助使用他们的 API。 它提供了一个similar_objects()方法,它返回一个对象列表,这些对象被标记为与特定对象相似,并按相似性降序排列。

来自docs:

similar_objects()
Returns a list (not a lazy QuerySet) of other objects tagged similarly to this one, ordered with most similar first. Each object in the list is decorated with a similar_tags attribute, the number of tags it shares with this object.

# returns list of objects tagged similarly
some_object.tags.similar_objects()

关于jquery - Django "Similar Objects"基于相似标签集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36952178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com