I am a little bit confused here but let me try. I think all this 
computation should be done one single time and the result should be store 
in the database. Whenever these results are required then we straight away 
fetch from db.
I would implement it like this

- Keywords would not be full strings/titles. They would be things like 
"Thriller", "Action", "Lovestory"
- Books would have a many to many field to keywords.
- User would have an interests field pointing to multiple keywords.
- There would be another model which store the user and his book 
recommendations.

Now the operation you are performing:
- The user would select his interests. We would trace these interests back 
to the books they are associated with.
- Now we create the objects of the "recommendations" model for each books 
for associated with an interest. If that book is already present just 
increase the points of that book (in that model object).
- when ever user changes his interests his recommendations would have to be 
changed accordingly.
- Now you operation should just boil down to fetching objects from db, the 
computation is a one time affair.


see if this works for you

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/55ee80e8-e883-4215-bf18-f0e9d1bfcb74o%40googlegroups.com.

Reply via email to