Suppose I have the following Django models:
class Article(models.Model): title = models.CharField(max_length=200) blog = models.CharField(max_length=255) rating = models.IntegerField(default=0) class ArticleTag(models.Model): article = models.ForeignKey(Article) tag = models.CharField(max_length=200)
Add some data:
ArticleID Rating Blog ----------------------------------------- article1 -> 1 3 CNN article2 -> 2 2 BBC article3 -> 3 5 BBC article4 -> 4 9 NTV ArticleID tag ------------------- 1 tag1 1 tag2 1 tag3 2 tag1 2 tag4 3 tag5 4 tag6 4 tag7
Suppose we have a user who likes tag1 , tag2 , tag6 and BBC . All articles are eligible because article1 has tag1 and tag2 , article 4 has tag1 , article2 and article3 are from the BBC .
If we order them by rating: article4 , article3 , article1 , article2 .
However, I need to order items by the number of matching tags that have a + blog, and then by rating as the second order parameter. Therefore, I expect results in the following order:
- article1 -
tag1 and tag2 , rating = 3 - article2 -
tag1 and BBC , rating = 2 - article4 -
tag6 , rating = 9 - article3 -
BBC , rating = 5
Can this be done in Django? If not, what about PostgreSQL?
source share