python - 属性错误 : 'bool' object has no attribute

标签 python django django-forms django-views

我正在处理一个 Django 项目,基本上我已经编写了一个存储用户详细信息的模型。他们在注册后完成此配置文件,我在用户模型中有一个 bool 值,说明他们是否已完成此自定义配置文件,以便我可以在模板中进行更改。

当提交第二个个人资料页面的表单时,我希望它将 Bool 从 False 更新为 True 但我收到错误:

'bool' object has no attribute 'has_created_artist_profile'

见下面的代码:

View .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', ]

任何人都能够提出一种更好的方法来更新这个/以摆脱错误?

最佳答案

'bool' object has no attribute 'has_created_artist_profile'表示您正在尝试访问属性 has_created_artist_profile bool 对象( TrueFalse )的,而不是对象的。

例如:True.has_created_artist_profile将产生完全相同的错误。

从您的代码中,您设置 artist_status到作为对象一部分的 bool 值( current_artist ),然后尝试从该 bool 值访问属性。

按照建议,您已删除 artist_status变量,您现在正在使用 current_artist直接对象。

关于python - 属性错误 : 'bool' object has no attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50360404/

相关文章:

python - 在 Python 中,如何找到排序列表中第一个大于阈值的值的索引?

python - 没有名为 numpy 的模块与 pypy

django - 如何在Django(django-admin)中检查值转换?

python - 模型表单中的 Django M2M 字段?

python - DropoutWrapper 在运行中是不确定的?

python - Matplotlib 堆积条形图 : need to swap x and height

django - 在 django-registration 中传播 ?next=

python - Django RateLimit 403 页面未被替换

django - 如何在 django 中翻译表单

python - 从数组生成 Django 表单