Issue
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?
Solution
'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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.