1

I am passing in a bunch of Project objects into my template as the variable 'projects'. Then I loop over each of them like this:

{% for project in projects %}
    <div class="conflict">
        <h2>{{ project.title }}</h2>
        <div class="conflictdata">
            <p>A conflict with *USER*</p>
            <p>Created on *DATE*</p>
            <p>MORE INFO HERE?</p>
        </div>
    </div>
{% endfor %}

Now the project models has a many to many relationship with the Django user model (but each project only has two users) (while every user can have many projects). What I would like to do is exclude the current logged in {{user}} from the Project.users queryset and display it in my template (because that would be the user with whom the current logged in user shares said project).

How could I achieve this?

Also here's my view in case it helps:

@login_required
def myconflicts(request):
    form = ProjectForm(request.POST or None)
    if request.method == "POST":
        form = ProjectForm(request.POST)
        if form.is_valid():
            project = form.save()
            project.users.add(request.user)
            project.users.add(User.objects.last())
            return redirect('problemdashboard:problem-dashboard', project_id=project.pk)
            form = NeedForm()
        else:
            form = NeedForm()
    return render(request, 'conflictmanagement/myconflicts.html', {
        'form': form,
        'projects': request.user.project_set.all()
         })
  • in your template use if condition to check if the username does not match the user from queryset .. if it matches do not print otherwise print. You can access the current username by this ```if request.user.username``` – Hashir May 17 '20 at 12:27
  • @muhammadhashirhassan but how do i get the queryset inside the template in order to do that? Could you please show me a quick example in an answer? – JustADudeTryingToCode May 17 '20 at 12:30
  • could please show your model class of project where you implemented many to many so I can give an example according to that? – Hashir May 17 '20 at 12:47

2 Answers2

1

I had to simply loop over all of the users of that project within the outer for loop and then check for each user whether it was the current logged in user, if it wasn't I printed it.

<p>A conflict with {% for currentuser in project.users.all %}
                {% if currentuser != user %}
                {{currentuser}}
                {% endif %}
                {% endfor %}</p>
  • Great! A Good programming practice is to compare just the unique key instead of whole object for example ```if currentuser.id = request.user.id``` because the comparator will only have to compare one unique attribute instead of all attributes. – Hashir May 17 '20 at 14:32
0

In your queryset, instead of request.user.project_set.all() use filter. To fulfill your condition you might be looking for negation because you want all the projects except the current user so your queryset will look something like this:

from django.db.models import Q # at the start

request.user.project_set.objects.filter(~Q(user.id = request.user.id))

where Q is used for negation. I personally have not used the negation filter but it works according to: https://stackoverflow.com/a/1154977/11979793

Hashir
  • 390
  • 5
  • 20