After reading 
1. Annotation
2. In field lookup
3. Aggregate function
4. Subquery
this is the best i come across.
#views.py
sub = Subquery(user[0].intrests.annotate(key=Lower('keyword')).values('key'
))
sponsored_books = models.SponsoredBook.objects.annotate(tit=Lower(
'keywords__title')).filter(tit__in=sub).annotate(points=Count('id')).
order_by('-points', '-bid')[:6]


On Sunday, June 14, 2020 at 11:26:10 AM UTC+5:30, Himanshu Pharawal wrote:
>
> Yes your assumptions are right.
> I am sorry for irrelevant models code i ma posting, please check in 
> re-post codes.
> #This code is use to match current user interests to 
> SponsoredBook.keywords and if it matches more than one keyword we increase 
> it's points and finally we sort this on the basis of points and pass it to 
> the templates.
> #For a small amount of data this works fine as will data will grow this is 
> going to make process slow. This is why i need a different optimal approach 
> to do this.
>
> #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)
>
> class SponsoredBook(models.Model):
>         verified = models.BooleanField(default=False)
>         title = models.CharField(max_length=50)
>
> class Keyword(models.Model):
>         sponsored_book = models.ForeignKey(SponsoredBook, related_name=
> 'keywords', on_delete=models.CASCADE)
>         title = models.CharField(max_length=50)
>
> #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)
>
>
>
> On Sunday, June 14, 2020 at 2:53:17 AM UTC+5:30, Gs_1001 wrote:
>>
>> Hey, I was having a bit trouble trying to understand the idea.
>>
>> - The SponsporedBooks model has user field which isn't being used here. 
>> Where do you plan to user this ?
>> - The CustomSponsoredBooks model does not have a user field. Are you not 
>> trying to store the points for the books (possible for recommendations) 
>> specific to every user. I see that you are calculating these are runtime 
>> does that mean the user interests are too volatile ?
>>
>> I think these things could clear things up
>>
>>
>> On Saturday, June 13, 2020 at 8:18:29 PM UTC+5:30, Himanshu Pharawal 
>> wrote:
>>>
>>> #This code is use to match current user interests to 
>>> SponsoredBook.keywords and if it matches more than one keyword we increase 
>>> it's points and finally we sort this on the basis of points and pass it to 
>>> the templates.
>>> #For a small amount of data this works fine as will data will grow this 
>>> is going to make process slow. This is why i need a different optimal 
>>> approach to do this.
>>>
>>> #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)
>>>
>>>

-- 
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/0ef2b6fa-3bda-4551-9280-cca26f112d51o%40googlegroups.com.

Reply via email to