2

I am trying to access my followers in the available module, I can get the number of count such as the number of followers and number of followings but I can't get the list of followers to see my followers. Need suggestions.

My module.py File:

from django.db import models

class FollowerFollowing(models.Model):
        user_from = models.ForeignKey('User', on_delete=models.SET_NULL, null=True, related_name='rel_from_set')
        user_to = models.ForeignKey('User', on_delete=models.SET_NULL, null=True, related_name='rel_to_set')
        created = models.DateTimeField(auto_now_add=True, db_index=True)

        class Meta:
            ordering = ('-created',)

        def __str__(self):
            return '{} follows {}'.format(self.user_from, self.user_to)


    User.add_to_class('following', models.ManyToManyField('User', through=FollowerFollowing, related_name='followers', symmetrical=False))

My views.py File:

def follower(request):
    print(request)
    if request.session['user_id']:
        query = FollowerFollowing.objects.filter(user_from=request.user)
        print(query)
        context = {
            'query': query
            }
        return render(request, 'Instagram_app/following.html', context)
    else:
        pass
    return HttpResponse("No user logged in")

follower.html (template)

{% extends 'Instagram_app/home.html'%}
{% block content %}
    <h2>following</h2>
    <div id="action-list">
        {% for result in query %}
            <h1>{{ result }}</h1>
        {% endfor %}
    </div>
{% endblock %}

Console error is:

(value, opts.object_name))
ValueError: Cannot query "mayur": Must be "User" instance.
[20/Jun/2018 11:45:27] "GET /Instagram_app/follower HTTP/1.1" 500 105165

Need suggestions to solve this problem. Thank you

2 Answers 2

3

Try to change this part:
(user_from=request.user)
To this:
(user_from=User.objects.get(username=request.user))
Your user_from must be a User model instance, not a simple username.

Sign up to request clarification or add additional context in comments.

Comments

0

Tried to change the code to display the following list according to logged in user session

from:

query = FollowerFollowing.objects.filter(user_from=request.user)

To:

query = FollowerFollowing.objects.filter(user_from=request.session['user_id'])

because request.user gives you the list of single and very first logged in user's following list for all users.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.