Reputation: 1736
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.
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
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
Upvotes: 1
Views: 165
Reputation: 18909
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.y
and you can reach x from y like this:
y.x_set
Have fun with django
Upvotes: 1