2012-09-19 21 views
1

我使用許多一對多的關係在我的應用我,我不能夠將數據提供給它是由Django的創建默認爲確保多對一表一對多relationships.It給出方法(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') 

我來到這裏打了兩天,但我解決不了,請幫助我。

謝謝

回答

1

你可以使用這樣的:

customer_check.users.add(your user instance) 

我認爲你要使用

user.customer_check_set. 

,但你只是用錯。

如果類x具有M2M領域y您可以從X例如直接到達Ÿ這樣

XY

,你可以從y中這樣達到X:

y.x_set

與django玩得開心

+0

我用那個,但它顯示**的錯誤,現在它顯示此int()參數必須是一個字符串或數字,而不是'SimpleLazyObject'** – Inforian

+0

customer_check.set_customers([user.id])更改它customer_check.users.add(用戶) - >>>不是用戶標識。認爲面向對象的人:)我們的工作對象不是他們的ID。 – ibrahimyilmaz

+0

我已經告訴過你,但它答覆我,全球名稱用戶沒有定義 – Inforian