如何使用多对多关系

时间:2023-01-02 20:17:06

I am using Many-to-many relationships in my app and I am not able to feed data into the table which is by default created by the Django to ensure the Many-to-many relationships.It gives the error in method (def Set_Checkout_Attributes(request):) that 'Customer_check_attributes' object has no attribute 'set_customers' If I replace set_customers with set_users the error will remain same.

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

The models which I used are:

我使用的模型是:

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=

My view.py is as

我的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')

I am got struck here for two days but I can't solve it Please help me.

我被打了两天但我无法解决它请帮助我。

Thanks

1 个解决方案

#1


1  

You can use like this:

你可以像这样使用:

customer_check.users.add(your user instance)

I think you are trying to use

我想你正在尝试使用

user.customer_check_set.

but you just use wrongly.

但你只是错误地使用。

if class x has M2M field y you can reach y directly from x instance like this

如果类x具有M2M字段y,则可以直接从x实例到达y,如下所示

x.y

and you can reach x from y like this:

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

y.x_set

Have fun with django

享受django的乐趣

#1


1  

You can use like this:

你可以像这样使用:

customer_check.users.add(your user instance)

I think you are trying to use

我想你正在尝试使用

user.customer_check_set.

but you just use wrongly.

但你只是错误地使用。

if class x has M2M field y you can reach y directly from x instance like this

如果类x具有M2M字段y,则可以直接从x实例到达y,如下所示

x.y

and you can reach x from y like this:

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

y.x_set

Have fun with django

享受django的乐趣