django'tuple'对象没有属性'id'

时间:2012-10-23 15:44:00

标签: django

这是我的观点,我称之为表格。当我提交表格时,我得到''tuple'对象没有属性'id'“

@login_required
def _login_save(request, form):
    if request.method == 'POST':
        form = LoginSaveForm(request.POST)
        if form.is_valid():
            # Create or get login.
            login1 = login.objects.get_or_create(
                                                 id=1 #this is for testing reasons to get a specific record
                                                )
            login1.name=form.cleaned_data['name']
            login1.loginUrl = form.cleaned_data['url']
            login1.password = form.cleaned_data['password']
            login1.login_username = form.cleaned_data['Login_username']
            login1.notes = form.cleaned_data['notes']
            login1.save()
    return login1

1 个答案:

答案 0 :(得分:4)

get_or_create(instance, created)的形式返回一个元组,其中created显然是一个布尔值,告诉你它是找到了对象还是必须创建它。使用get_or_create时,最好立即解压缩:

login1, created = login.objects.get_or_create(...)

然后,您可以按照您的意愿使用login1