# This code is use for match the interests of current user to saved SponsoredBook-keywords # If same book match with two keywords we increase it's priority by increasing it's points and finally sort them on the basis of their points
#Please reach out to me if you need more information on the same #models.py - my model looks something like this class Intrest(models.Model): user = models.ForeignKey(User, related_name='intrests', on_delete=models.CASCADE) keyword = models.CharField(max_length=100) created_on = models.DateTimeField(auto_now_add=True) def __str__(self): return self.keyword class Meta: ordering = ['-created_on'] class SponsoredBook(models.Model): places = ( ('Royal Place', 'Royal Place'), ('Search Result', 'Search Result') ) def validate_image(fieldfile_obj): if get_image_dimensions(fieldfile_obj) != (180, 290): raise ValidationError("Thumnail should be exact 180*290") user = models.ForeignKey(User, related_name='sponsored_books', on_delete=models.CASCADE) def user_directory_path(self, filename): return 'media/user_{0}/{1}'.format(self.user.id, filename) title = models.CharField(max_length=20) bid = models.FloatField() placed_on = models.CharField(choices=places, max_length=20) description = models.TextField() height = models.PositiveIntegerField() width = models.PositiveIntegerField() verified = models.BooleanField(default=False) thumbnail = models.ImageField(upload_to=user_directory_path, height_field='height', width_field='width', validators=[validate_image]) created_on = models.DateTimeField(auto_now_add=True) def __str__(self): return str(self.title) class Meta: ordering = ['-bid'] class Keyword(models.Model): sponsored_book = models.ForeignKey(SponsoredBook, related_name='keywords', on_delete=models.CASCADE) title = models.CharField(max_length=50) def __str__(self): return self.title class Meta: ordering = ['title'] #views.py class CustomSponsoredBook(): obj = models.SponsoredBook.objects.none() points = 0 user = get_object_or_404(User, id=3) intrests = user.intrests.all() final_sponsored_books = [] count = 0 for intrest in intrests: print(intrest) sponsored_books = models.SponsoredBook.objects.filter(verified=True, keywords__title__iexact=intrest) print(sponsored_books) if sponsored_books.exists(): sponsored_books = list(sponsored_books) for sponsored_book in sponsored_books: should_continue = False for custom_sponsored_book in final_sponsored_books: if custom_sponsored_book.obj == sponsored_book: custom_sponsored_book.points += 1 should_continue =True break if should_continue: continue custom_sponsored_book = CustomSponsoredBook() custom_sponsored_book.obj = sponsored_book final_sponsored_books.append(custom_sponsored_book) if len(final_sponsored_books) == 6: break final_sponsored_books.sort(key=lambda x: x.points, reverse=True) # For the small amount of data in Intrests and SponsoredBook-Keywords table this is going to work but as the data will grow this will the process slow, so i need an alternative solution (mathematical or diffrent data structure or different approach) to get the same result -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/df1125c0-5db1-4441-baf2-b21ce2d6ee8ao%40googlegroups.com.