Django taggit标签和标签计数基于应用于模型的过滤器

时间:2018-06-06 19:23:18

标签: django django-taggit

我的/** * Object to composite certain user properties * @typedef {RealUser} * @property {String} user The local string for the user * @property {User} realUser The user that Discord gives us * @property {Number} score The score this user has */ /** * Class to encapsulate user and score and data */ class Game { /** * Constructs a game */ constructor() { /** * The users we are keeping score of * @type {Object} */ this.users = {}; } /** * Get the score of a particular user * @param {String} user User to get score of * @returns {Number} User's score */ getScore(user) { return this.users[user] || 0; } /** * Get a composite of users and their status * @param {String[]} users The users to put on our leaderboard * @returns {Promise<RealUser[]>} Sorted list of users that we included in our leaderboard */ getLeaderBoard(users) { // Map all the users that we are given to Promises returned bye fetchUser() const allRealUsersPromise = Promise.all(users.map(user => client.fetchUser(user) /* * Create an object that will composite the string that we use * to note the user locally, the Discord User Object, and the * current score of the user that we are tracking locally */ .then(realUser => ({ user, realUser, score: this.getScore(user) })))); /* * Once we have all the data we need to construct a leaderboard, * we should sort the users by score, and hand back an array * of RealUsers which should contain all the data we want to * print a leaderboard */ return allRealUsersPromise .then(scoredUsers => scoredUsers.sort((a, b) => a.score - b.score)); } /** * Prints out a leaderboard * @param {String[]} users The users to include on our leaderboard */ printLeaderBoard(users) { // Go get a leaderboard to print this.getLeaderBoard(users).then(sortedScoredUsers => { // Iterate our RealUsers sortedScoredUsers.forEach((sortedScoredUser, idx) => { const username = sortedScoredUser.realUser.username; const score = sortedScoredUser.score; // Print out their status console.log(`${username.substring(0, 13)} is in position ${idx + 1} with ${score} points`); }); }); } } const game = new Game(); game.users["bob"] = 5; game.users["sue"] = 7; game.users["tim"] = 3; game.printLeaderBoard(Object.keys(game.users)); 模型包含类别和其他字段以及标记(使用BlogPost)。现在,下面是我的示例对象。

django-taggit

现在我想根据类别获取最常见的blogpost标签,例如o1 = BlogPost(category='c1', title='some title') o1.tags.add('tag1', 'tag2', 'tag3') o1.save() o2 = BlogPost(category='c1', title='some title') o2.tags.add('tag1', 'tag2') o2.save() o3 = BlogPost(category='c2', title='some title') o3.tags.add('tag1', 'tag2', 'tag3') o3.save() category c1tag1最常见且计数为tag2,{{1 }}

所以我使用了2。得到2BlogPost.tags.most_common().filter(blogpost__category='c1').annotate(tag_count=Count('name')),但计数倍增。如何查询此方案。

0 个答案:

没有答案