每当应用搜索时,增加模型的计数字段 [英] Increasing the count field of model whenever a search is applied
本文介绍了每当应用搜索时,增加模型的计数字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个搜索API
class SearchViewSet(RetrieveModelViewSet):
serializer_class = ArticleSerializer
queryset = Article.objects.all()
query = self.request.query_params.get("query")
final_queryset = search(query,queryset,#some logic)
#logic to generate serialiser and return serialiser.data
serialiser = self.get_serializer(final_ueryset, many=True)
search
函数返回文章列表,即
search
function returns a list of articles i.e
type(final_queryset) is List
然后。我也不会退回商品order_by(’count)。
现在,我不想增加final_queryset中排名前3的文章的计数
了。
And. I wan't to return the articles order_by('count) as well.
Now I wan't to increase the count
of top 3 articles from the final_queryset is there a way of doing this.
推荐答案
想出了一种实现此
的方法,只需在列表上进行迭代并增加 count
列表中的对象
Figured out a way for doing this
Just simply iterate on the list and increment the count
of the objects in the list
class SearchViewSet(RetrieveModelViewSet):
serializer_class = ArticleSerializer
queryset = Article.objects.all()
query = self.request.query_params.get("query")
final_queryset = search(query,queryset,#some logic)
# to increment the count of top 3 entity_aliases
for instance in final_queryset[:3]:
instance.count += 1
instance.save()
#logic to generate serialiser and return serialiser.data
serialiser = self.get_serializer(final_ueryset, many=True)
这篇关于每当应用搜索时,增加模型的计数字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文