Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Django:The QuerySet value for an exact lookup must be limited to one result using slicing

Ask Question

I am working on a project where admin can assign team to manager. But it is not working and i have no idea how it will work. Because it is raising an error saying "The QuerySet value for an exact lookup must be limited to one result using slicing."

Here is my model.py

class manager(models.Model):
name = models.CharField(max_length= 500)
designation = models.CharField(max_length= 500)
user = models.ForeignKey(User,on_delete=models.CASCADE)
class Meta:
    permissions = [
        ("edit_task", "can edit the task"),

here is my views.py file for the teams of manager

@login_required (login_url= 'have the url where it will go')
@permission_required('have the permission that is assigned by me')
def supervisor(request):
return render(request, 'manager/index-3.html')
def supervisor_team(request):
print(request.user.email)
email=request.user.email
obj= Create_Team.objects.filter(status='Accept', 
managers=manager.objects.filter(user__email=email))
return render(request, "manager/accept_team.html", {"object": obj})

here is my template

<div class="body table-responsive">
                        <table class="table table-hover">
                            <thead>
                                    <th>S No.</th>
                                    <th>COMPANY NAME</th>
                                    <th>TEAM MEMBER</th>
                                    <th>EMAIL</th>
                            </thead>
                            <tbody>
                                    {%for object in team%}
                                        <form id="form_id" method="POST" action = "#">
                                            {% csrf_token %}
                                    <th scope="row"> {{ forloop.counter }}</th>
                                    <td>{{object.company_name}}</td>
                                    <td>{{object.team_member}}</td>
                                    <td>{{object.email}}</td>
                                {% endfor %}
                            </tbody>
                        </table>

I have no idea where i am wrong.here is the image of the error

Please share your Create_Team model. I guess you are passing queryset of manager but need to pass one instance of manager` – Sergey Pugach May 15, 2019 at 13:15
obj= Create_Team.objects.filter(status='Accept', 
managers=manager.objects.filter(user__email=email))

if you want to filter by one manager you have to use get instead of filter:

obj= Create_Team.objects.filter(status='Accept', 
managers=manager.objects.get(user__email=email))

But if you want to filter by several managers you need to use __in:

obj= Create_Team.objects.filter(status='Accept', 
managers__in=manager.objects.filter(user__email=email))

Also you are passing {"object": obj}) to template but in templare you are trying to iterate over team. So change it to pass team variable:

return render(request, "manager/accept_team.html", {"team": obj})
                yes it works for me but i have an another problem that the teams is not showing in the UI
– Sahil Sharma
                May 15, 2019 at 13:20
                So please add your template as well here. And print obj before rendering in order to check if it's empty or not.
– Sergey Pugach
                May 15, 2019 at 13:23
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.