AttributeError: 'bool' object has no attribute

Question:

I am working on a Django project and essentially I have written a model which stores user details. They complete this profile after they have signed up and I have a Boolean in the User model which states whether they have completed this custom profile yet so I can make changes in the template.

When the form for the second profile page gets submitted I would like it to update the Bool from False to True but I am getting the error:

'bool' object has no attribute 'has_created_artist_profile'

See code below:

views.py

def ArtistEditView(request):
    artist = Artist.objects.get(user=request.user)
    current_artist = request.user
    artist_status = current_artist.has_created_artist_profile

    if request.method == 'POST':
        form = ArtistForm(request.POST, request.FILES, instance=artist)
        if form.is_valid():
            artist_status.has_created_artist_profile = True
            artist_status.save()
            form.save()
            return redirect(reverse('artist_home'))
    else:
        artist_dict = model_to_dict(artist)
        form = ArtistForm(artist_dict)
    return render(request, 'artist/artist_edit.html', {'form': form})

forms.py

class ArtistForm(forms.ModelForm):
    class Meta:
        model = Artist
        exclude =['user', ]

Any one able to suggest a better way to update this / to get rid of the error?

Asked By: Adam Dalton

||

Answers:

'bool' object has no attribute 'has_created_artist_profile' means you’re trying to access the attribute has_created_artist_profile of a boolean object (True or False), rather than that of an object.

For example:
True.has_created_artist_profile will produce the exact same error.

From your code, you set artist_status to that of a boolean which was part of an object (current_artist), and then tried to access a property from that boolean.

As advised, you have removed the artist_status variable, and you are now using the current_artist object directly.

Answered By: Robert Seaman

The Python "AttributeError: 'bool' object has no attribute" occurs when we try to access an attribute on a boolean value (True or False). To solve the error, track down where you are setting the value to a boolean or use the hasattr() method to check for the attribute’s existence.

Here is how the error occurs.

 artist_status.has_created_artist_profile = True

You are trying to access an attribute on a boolean (True or False) instead of on the object. If you print() the value you are accessing the attribute on, it will be a boolean. So you need to track down where exactly you are setting the value to a boolean in your code and correct the assignment.

Answered By: Hilario Nengare