Luis A. Borit Guitton Luis A. Borit Guitton - 3 months ago 28
Python Question

int() argument must be a string or a number, not 'SimpleLazyObject' django

I'm passing what is necessary for reach the follow function but this is not working for some reason

Error: TypeError at /follow/1/


in views.py

def follow(request, pk):
following, created = Following.objects.get_or_create(
follow_from_id=request.user,
follow_to_id=pk)
return redirect('login')


in models.py

class Following(models.Model):
follow_from = models.ForeignKey("auth.User",related_name='from_person')
follow_to = models.ForeignKey("auth.User", related_name='to_person')
date_follow = models.DateTimeField(auto_now=True)

def __unicode__(self):
return unicode(self.date_follow)

def __str__(self):
return self.date_follow


template

<a href="{% url 'follow' l.pk %}">


in urls.py

url(r'^follow/(?P<pk>\d+)/$', follow, name='follow'),

Answer

follow_from field is a foreign key to the user model. In your filter, you are comparing the ID with an user instance.

Your filter follow_from, should be as it:

follow_from=request.user

But, if you want to compare by id, extract the id from the user instance

follow_from_id=request.user.id
Comments