得到错误' QuerySet'对象没有属性' _meta'

时间:2018-02-21 09:45:08

标签: django python-3.x

收到此错误,

'查询集'对象没有属性' _meta'

如何解决这个问题

views.py

def profile_update_view(request, username):
    user = User.objects.filter(username__iexact=username)
    form = UserProfileForm(instance=user)

    if request.user.is_authenticated and request.user.id == user.id:
        if request.method == "POST":
            form = UserProfileForm(request.POST, request.FILES, instance=user)

            if form.is_valid():
                created_prof = form.save(commit=False)
                created_prof.user = request.user
                created_prof.save()

                return redirect('profiles:profile-detail',username=username)

        return render(request, "profiles/profile_form.html", {
            "username": username,
            "form": form,
        })

1 个答案:

答案 0 :(得分:0)

filter始终返回一个查询集。您应该使用get来获取单个实例。

user = User.objects.get(username__iexact=username)