I need to know how to optimize this?

2020-06-13 Thread Himanshu Pharawal
# 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.


I need to know how to optimize this?

2020-06-13 Thread Himanshu Pharawal
#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/617d9acc-57a8-4987-93f3-1f1cf8f37b91o%40googlegroups.com.


Regarding sending the link to reset the password

2020-06-13 Thread Devendra reddy
Hi to all

 I am working on the api side using python django.now i have the one
requirement. i want  to send the link to reset the password while clicking
on the forgot password link on the ui. from api side  i want to send the
mail by the link of reset password to the respected user.please help me out
to complete this.


Thanks and regards,
Devendra.

-- 
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/CABB0Uf1%3Dy0p8%3D%3D65EkdFB4_XRMwhcp84OAeQxem%2Bw94S2aw7LA%40mail.gmail.com.


Re: Regarding sending the link to reset the password

2020-06-13 Thread Agnese Camellini
I have set a temporary password when they give me the email. By the update
on the user i check in the password field if there is the temporary
password and if it is there i send the reset link by email.
I have an api with django rest framework and django.
Agnese

Il Sab 13 Giu 2020, 20:44 Devendra reddy  ha
scritto:

> Hi to all
>
>  I am working on the api side using python django.now i have the one
> requirement. i want  to send the link to reset the password while clicking
> on the forgot password link on the ui. from api side  i want to send the
> mail by the link of reset password to the respected user.please help me out
> to complete this.
>
>
> Thanks and regards,
> Devendra.
>
> --
> 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/CABB0Uf1%3Dy0p8%3D%3D65EkdFB4_XRMwhcp84OAeQxem%2Bw94S2aw7LA%40mail.gmail.com
> 
> .
>

-- 
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/CACXuh-Q_Tz-qcg9o0B%3DiGvfvVC--3u3vmzu9hiu%3DAAQNWRtVrg%40mail.gmail.com.


How to add a user ratings feature in djnago application?

2020-06-13 Thread learn code
Hello Everyone,

Can anyone tell me how to add a star rating feature in Django application?

-- 
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/ec01e0d3-78d3-4bd4-9dc0-ff9ec0da1866o%40googlegroups.com.


Re: How to add a user ratings feature in djnago application?

2020-06-13 Thread Rupesh Dahal
You can save rating on database and show them with the help of JS.

On Sun, Jun 14, 2020 at 12:52 AM learn code  wrote:

> Hello Everyone,
>
> Can anyone tell me how to add a star rating feature in Django application?
>
> --
> 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/ec01e0d3-78d3-4bd4-9dc0-ff9ec0da1866o%40googlegroups.com
> 
> .
>

-- 
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/CAC-WMxm2MosgKr4amv5A0SkWOXGGOWxLXKn3Dya_o%2BAHk7WymQ%40mail.gmail.com.


Re: How to add a user ratings feature in djnago application?

2020-06-13 Thread learn code
Thank you for your reply.
Can you tell me more details how can I do that?


On Sat, Jun 13, 2020, 12:21 PM Rupesh Dahal  wrote:

> You can save rating on database and show them with the help of JS.
>
> On Sun, Jun 14, 2020 at 12:52 AM learn code 
> wrote:
>
>> Hello Everyone,
>>
>> Can anyone tell me how to add a star rating feature in Django application?
>>
>> --
>> 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/ec01e0d3-78d3-4bd4-9dc0-ff9ec0da1866o%40googlegroups.com
>> 
>> .
>>
> --
> 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/CAC-WMxm2MosgKr4amv5A0SkWOXGGOWxLXKn3Dya_o%2BAHk7WymQ%40mail.gmail.com
> 
> .
>

-- 
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/CAPUBRGUHS1Q4MQrzwS64WA7pXnmCWfDgtQ-LhmUAeLwQTL6C9g%40mail.gmail.com.


Django - How works different level users and its authorizations

2020-06-13 Thread Jayakumar Selvan
Hi Friends,

I need some information about Django.
Basically i'm PHP developer. nowadays moving to python with micro services. 
But i need to know how to handle user authorization in Django. 
For example is,

The site have two different users logins. One for front users and site 
administrators users.
The front users have multiple users level and also administrator panel also 
different levels there.

The front user means like one client, the client have multiple users like 
employees based on the level the page will available.

Coming to admin panel also the site owner also have multiple employees, and 
the employees also have different roles. 

Coming to me what was we used in earlier for database table structures is, 
like 

Front Users Table:
1. users
2. roles
Site Admin Panel Table:
1. admin_users
2. admin_roles

In Django have inbuilt administrator panel and also have different level 
authorization.
I'm confused how to handle this. I want to create a users and roles table 
and want handle sessions in differently or i want use existing Django 
inbuilt admin users tables???

Please clarify this, it will help me lot and moving further things. 

Thanks for reading this.

-- 
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/5a893292-b7d0-4dfd-80ea-2557fbb21490o%40googlegroups.com.


Re: Regarding sending the link to reset the password

2020-06-13 Thread Kasper Laudrup

Hi Devendra,

On 13/06/2020 20.43, Devendra reddy wrote:

Hi to all

  I am working on the api side using python django.now i have the one 
requirement. i want  to send the link to reset the password while 
clicking on the forgot password link on the ui. from api side  i want to 
send the mail by the link of reset password to the respected user.please 
help me out to complete this.




https://simpleisbetterthancomplex.com/tutorial/2016/09/19/how-to-create-password-reset-view.html

Kind regards,

Kasper Laudrup

--
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/4d0da0e5-70c9-daac-f3a3-4d4bb173242c%40stacktrace.dk.


Re: I need to know how to optimize this?

2020-06-13 Thread Gs_1001
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 ?

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)
>
>
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, f

Re: I need to know how to optimize this?

2020-06-13 Thread Gs_1001
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/a1c70dfe-213b-4074-92e9-34a814e4aa13o%40googlegroups.com.


Re: How to add a user ratings feature in djnago application?

2020-06-13 Thread Gs_1001
Hey, 

A rating feature for a peer reviewed books website would look somewhat like:
- A books model having "no_of_reviews" and "rating" field

Every time a user rates a book no_of_reviews field would be incremented and 
rating field would be updated.

If you think I somehow missed the point then please let me know.

On Sunday, June 14, 2020 at 12:37:05 AM UTC+5:30, learn code wrote:
>
> Hello Everyone,
>
> Can anyone tell me how to add a star rating feature in Django application?
>

-- 
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/dcce83fc-5925-4587-9ed6-d6884cd85b39o%40googlegroups.com.


Updating a date field

2020-06-13 Thread Clive Bruton
I am trying to update an existing date field, however when I do that  
I get the following exception:


Exception Type: TypeError
Exception Value: expected string or bytes-like object

The query looks like this:

queryset = Item.edit.filter(**kwargs).update 
(expires=set_expires,is_active=True)


and set_expires looks like this:

def set_expires():
return datetime.now(timezone.utc) + timedelta(days=30)

I am using "set_expires" in the model for "Item", to set the initial  
value, without issue. Do I have to convert to a string when doing an  
update?


Thanks


-- Clive

--
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/8B6B6771-0E9C-4E4A-A696-18CF9B7ED355%40indx.co.uk.


Re: I need to know how to optimize this?

2020-06-13 Thread Himanshu Pharawal
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=p