1

这是我的观点,我用一种形式来称呼它。当我提交表单时,我得到“'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
4

1 回答 1

5

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

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

然后,您可以login1按照您的假设使用。

于 2012-10-23T15:49:50.230 回答