我有一个模型Order
,
class Order(SmartModel):
#each individual status
SUBMITTED = 1 # the credit card was valid or mobilemoney was recieved.It is ready for us to process the order
PROCESSED = 2 # After submitted orders are reviewed, we can mark them as processed, letting deliverers know order is ready to be shipped
DELIVERED = 3 # the order has been processed and approved by the adminstrator(in this case us), it is delivered.
PICKED_UP =4 # the order has been processed and is picked up by customer
CANCELLED = 5 # Customer called the company and decided they didnt want to go through with the order either by phone or email.
# SET OF POSSIBLE STATUSES
ORDER_STATUSES = ((SUBMITTED,'Submitted'),(PROCESSED,'Processed'),(DELIVERED,'Delivered'),(PICKED_UP,'picked_up'),(CANCELLED,'Cancelled'),)
#Order info
date = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=ORDER_STATUSES, default=SUBMITTED)
customer = models.ForeignKey(Customer,help_text="The customer who made this order",default=1)
restaurant = models.ForeignKey(Restaurant,default = '1',help_text="The restaurant the customer order from")
如您所见,我已将customer a foreignkey
默认设置为1
. 这是Customer
模型
class Customer(SmartModel):
#contact info
email = models.EmailField(max_length=50,help_text="Needed as alternative")
mobile = PhoneNumberField(max_length=20,default='+25078######',help_text="Needed to communicate and confirm payment from mobile money")
#billing information
billing_name= models.CharField(max_length=50,help_text="Needed so we can deliver to the right individual")
billing_address= models.CharField(max_length=50,help_text="Needed for delivery purposes, should be office address.")
billing_city = models.CharField(ma
x_length=50,help_text="F4L services are only in selected cities.")
def __unicode__(self):
return "%s %s %s %s %s" % (self.email,self.mobile,self.billing_name,self.billing_address,self.billing_city)
这是我验证表格的方式。
def show_checkout(request):
if order.is_empty(request):
cart_url = urlresolvers.reverse('order_index')
return HttpResponseRedirect(cart_url)
if request.method == 'POST':
postdata = request.POST.copy()
form = forms.CheckoutForm(request.POST,postdata)
if form.is_valid():
anon_user = User.objects.get(id=settings.ANONYMOUS_USER_ID)
obj = form.save(commit=False)
obj.created_by = anon_user
obj.modified_by = anon_user
obj.save()
if postdata['submit'] == 'place order':
reciept_url = urlresolvers.reverse('checkout_reciept')
return HttpResponseRedirect(reciept_url)
else:
form = forms.CheckoutForm
context = {
'form':form,
}
return render_to_response('checkout/checkout.html',context,context_instance=RequestContext(request))
在此功能中,我继续创建客户并将其添加到客户,
def add_to_order(request,obj):
postdata = request.POST.copy()
#get quantity added, return 0 if empty
quantity = postdata.get('quantity',0)
# fetch the item or return missing page error_message
i = get_object_or_404(Item,pk=obj.id)
# order = get_object_or_404(Order,pk=obj.id)
# get items in order
order_items = get_order_items(request)
item_in_orders = False
# check to see if item is already in order
for order_item in order_items:
if order_item.item.id == i.id:
#update the quantity if found
order_item.augment_quantity(quantity)
item_in_orders = True
if not item_in_orders:
# creat and save a new order item
anon_user = User.objects.get(id=settings.ANONYMOUS_USER_ID)
try:
# customer = Customer.objects.get(pk=customer_id)
order= Order.objects.get(pk=order_id)
# restaurant = Restaurant.objects.get(pk=restaurant_id)
except:
customer = Customer.objects.create(created_by=anon_user,modified_by=anon_user)
# customer.save()
# import pdb;pdb.set_trace()
# restaurant = Restaurant(created_by=anon_user,modified_by=anon_user)
# restaurant.save()
order = Order.objects.create(customer=customer,created_by=anon_user,modified_by=anon_user)
# order.save()
# import pdb;pdb.set_trace()
oi=OrderItem.objects.create(order=order,shopping_id=_shopping_id(request),
quantity=quantity,
item=i,
created_by=anon_user,
modified_by=anon_user)
oi.save()
创建新订单项时调用此函数
def show_item(request,id):
# need to evaluate the HTTP method
if request.method == 'POST':
# import pdb;pdb.set_trace()
a = Item.objects.get(pk=id)
form = PartialOrderItemForm(request.POST,instance=a)
# check validation of posted data
if form.is_valid():
order.add_to_order(request,a)
# if test cookie worked, get rid of it
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
url =urlresolvers.reverse('order_index')
# redirect to order page
return HttpResponseRedirect(url)
else:
form = PartialOrderItemForm()
request.session.set_test_cookie()
context={
# 'categories':categories,
'form':form,
# 'menu':menu,
}
return render_to_response('item.html',context,context_instance=RequestContext(request))
问题是当我在浏览器中提交客户表单时,在我的管理员中customer
,每次不同的提交总是返回相同的数据。
为什么会这样?