Michael Romrell
Michael Romrell

Reputation: 1015

Django Error: 'bool' object has no attribute '__getitem__'

I'm getting the following Django error when I add a filter onto my view.

'bool' object has no attribute '__getitem__'

Here's the View, and if I remove .filter(Proposal.user == request.user) then everything works fine. Additionally if I print Proposal.user and request.user they are the same.

# views.py
def my_proposal_list(request):
    response = {}
    response['proposal_list'] = Proposal.objects.all().filter(Proposal.user == request.user)
    return render(request, "my-proposals.html", response)

And here is the Proposal Model. It's supposed to be returning and filtering:

# Models.py
class Proposal(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=50, verbose_name='Name Your Proposal')
    description = models.CharField(max_length=200, help_text='Who is this proposal for? What does it do?')
    create_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "%s %s" % (self.title, self.user)

What am I doing wrong? I don't know which Boolean object it's referencing. Your help would be greatly helpful! Thanks!

Upvotes: 2

Views: 10669

Answers (2)

Alex
Alex

Reputation: 8539

You've got double = that is throwing off your code.

Change it to:

response['proposal_list'] = Proposal.objects.all().filter(Proposal.user=request.user)

Upvotes: 0

alecxe
alecxe

Reputation: 473843

You should use a single = sign for filter() arguments:

Replace:

.filter(Proposal.user == request.user)

with:

.filter(user=request.user)

Note that you can also omit Proposal. and use just user here.

Also, there is no need for all() since you are using filter() afterwards:

response['proposal_list'] = Proposal.objects.filter(user=request.user)

Upvotes: 3

Related Questions