S
S
Sergey Yelpashev2019-11-15 00:53:46
Django
Sergey Yelpashev, 2019-11-15 00:53:46

Why does 'NoneType' object has no attribute 'user' error?

For some reason, the object becomes NoneType even though there is a check before that:

urls = Info.objects.filter(url=url)
if urls is not None:
   if urls.first().user != request.user:

Answer the question

In order to leave comments, you need to log in

2 answer(s)
_
_, 2019-11-15
@MrSedan

if urls is not None- always True because it .filter(url=url)returns a Queryset - it's always not None.
Use Info.objects.filter(url=url).first()- to get an object or None
If you need to check if there is something in the queryset use .exists()

R
Roman, 2019-11-15
@skipirich

try like this

urls = Info.objects.filter(url=url).first()
if urls is not None:
   if urls.user != request.user:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question