I have a simple follow/following setup running.

When a user (request.user) see's an object she likes, she can click the follow button and follow that user.

When she returns I want that button on the object to now not be enabled cause she is already following that user.

What is happening is that in the background, the follower/followee record is being made. The object in question has the id of the followee. I just can't figure out how to add that representation to the object_list.

In REST I would add a field to the serializer and that would take care of it. I could then evaluate the truthiness of the new field.

Any ideas on how to accomplish this?

1

There are 1 best solutions below

0
On

You should do it as a separate query and make the test in the template.

view:

def objects_list(request):
    ...
    return render(request, "the_template_path.html", {
         'objects': ObjectName.objects.all()[0:100],
         'followed_object_ids': ObjectName.objects.filter(follower=request.user).values_list('id', flat=True)
    })

template:

{% for object in objects %}
   {% if object.id in followed_object_ids %}
       ...
   {% else %}
       ...
   {% endif %}
{% endfor %}

That will get you started. Obviously you don't want to use generic names like object.

It would then probably be better to move the followed_object_ids query in a middleware so you don't always do it (really useful if you have featured objects widgets everywhere for instance).