私は2つのモデルOrder
とを持っていOrderItem
ます。
注文するプロセスはモデルから始まりOrderItem
ます。
class OrderItem(SmartModel):
shopping_id = models.CharField(max_length=50,db_index=True)
quantity = models.IntegerField(default=0)
item = models.ForeignKey(Item)
order = models.ForeignKey(Order,null=True,blank=True)
OrderItem
アイテムを表し、1つのフィールドがquantity
他のフィールドであるモデルフォームですexcluded
。フォームを検証して、そのようにアイテムを作成します。
def show_item(request,id):
# need to evaluate the HTTP method
if request.method == 'POST':
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={
'form':form,
}
return render_to_response('item.html',context,context_instance=RequestContext(request))
is_valid
ieの後に呼び出される関数はorder.add_to_order
、アイテムを作成して保存します。
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)
# 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)
oi=OrderItem.objects.create(shopping_id=_shopping_id(request),
quantity=quantity,
item=i,
created_by=anon_user,
modified_by=anon_user)
oi.save()
顧客が(データベースで)アイテムの作成を完了すると、次のフォームに入力します。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,null=True,blank=True,help_text="The customer who made this order",default=None,)
restaurant = models.ForeignKey(Restaurant,null=True,blank=True,default = None,help_text="The restaurant the customer order from")
#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(max_length=50,help_text="F4L services are only in selected cities.")
Order
は、そのように検証して保存するモデルフォームです。
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))
djangoのドキュメントhttps://docs.djangoproject.com/en/dev/topics/db/で提案されているように、私に関連するすべてのオブジェクトを返すために混乱が生じている場所の多くのOrderItem
前に呼び出されることを指摘する必要がありますクエリ/#following-relationships-backwardOrder
OrderItem
Order
>>> from orders.models import OrderItem,Order
>>> a = Order.objects.get(id=1)
>>> a
<Order: blah blah blahbaah@blahblah.com +250780000000 1 2013-02-26 17:25:23.138738+00:00>
>>> a.orderitem_set.all()
[]
外部キーを保存しなかったので、空だと思っていますが、どうすればよいか本当に混乱しています。どんな助けでも大歓迎です。