1

我在我的应用程序中使用多对多关系,我无法将数据输入到默认情况下由 Django 创建的表中以确保多对多关系。它在方法中给出错误(def Set_Checkout_Attributes (请求):)'Customer_check_attributes'对象没有属性'set_customers'如果我用set_users替换set_customers,错误将保持不变。

我使用的模型是:

class Customer(models.Model):
    user        =models.OneToOneField(User)
    birthday    =models.DateField()

class Customer_check_attributes(models.Model):
    users       =models.ManyToManyField(User)
    billing_add =models.CharField(max_length=100, blank=True , null=

我的 view.py 是

def CustomerRegistration(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/profile/')
    if request.method == 'POST':
        form = Registration_Form(request.POST)
        if form.is_valid():
            user=User.objects.create_user(username=form.cleaned_data['username'], email=form.cleaned_data['email'], password = form.cleaned_data['password'])
            user.first_name = form.cleaned_data['first_name']
            user.last_name = form.cleaned_data['last_name']
            user.save()



            customer=Customer(user=user, website=form.cleaned_data['website'], birthday=form.cleaned_data['birthday'], store=form.cleaned_data['store'], welcomemail=form.cleaned_data['welcomemail'])  
            customer.save()

                    return HttpResponseRedirect('/profile/')
        else:
            check_form=Check_Attribute_Form()
            context={'form':form, 'check':check_form}
                return render_to_response('customer/customer_register.html',context , context_instance=RequestContext(request)) 
    else:
        ''' user is not submitting the form, show them a blank registration form '''

            form = Registration_Form()
        check_form = Check_Attribute_Form()
            context={'form':form,'check':check_form}
            return render_to_response('customer/customer_register.html',context , context_instance=RequestContext(request))


####################################### checkout attributes ##################################################


def Checkout_Attributes(request):



    check_form = Check_Attribute_Form()
    context={'form':check_form}
    return render_to_response('customer/checkout.html',context,context_instance=RequestContext(request))

def Set_Checkout_Attributes(request):

    #if request.user.is_authenticated():
        #return HttpResponseRedirect('/checkout/')

    if request.method == 'POST':
            check_form = Check_Attribute_Form(request.POST)
            #if check_form.is_valid():
            customer_check=Customer_check_attributes(billing_add=check_form.data['billing_add'],shipping_add=check_form.data['shipping_add'],payment_method=check_form.data['payment_method'],shipping_method=check_form.data['shipping_method'],reward_points=check_form.data['reward_points'])
            customer_check.save()
            customer_check.set_customers([user.id])
                    return HttpResponseRedirect('/profile/')
        #else:
            #check_form=Check_Attribute_Form()
            #return render_to_response('a.html',{'check_form':check_form} , context_instance=RequestContext(request)) 
    else:
            return render_to_response('f')

我在这里被击中了两天,但我无法解决它请帮助我。

谢谢

4

1 回答 1

1

你可以这样使用:

customer_check.users.add(your user instance)

我想你正在尝试使用

user.customer_check_set.

但你只是用错了。

如果类 x 有 M2M 字段 y 你可以像这样直接从 x 实例到达 y

xy

你可以像这样从 y 到达 x :

y.x_set

与 django 玩得开心

于 2012-09-19T06:23:13.073 回答