0

我有两个模型OrderOrderItem.

下订单的过程从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代表一个项目并且是一个模型表单,其中一个字段是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在ieorder.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))

我应该指出,它OrderItem是在 .. 之前调用Order的 .. 大部分混淆来自于返回与我OrderItem相关的所有对象,正如 django 文档https://docs.djangoproject.com/en/dev/topics/db/所建议的那样查询/#following-relationships-backwardOrder

>>> 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()
[]

我认为它是空的,因为我没有保存外键,但我真的很困惑如何去做。任何帮助表示赞赏..

4

1 回答 1

0

您只需要传入(或以某种方式获取)订单的对象。

假设订单的 PK 在您的 POST 变量中,您可以这样做:

def add_to_order(request,obj):  
    postdata = request.POST.copy()
    order = Order.objects.get(pk=postdata.get('order_id'))
    …
        oi=OrderItem.objects.create(order=order, shopping_id=_shopping_id(request), …)

不过,根据您在下面的评论,您是在创建orderAFTER 之后创建的orderitem,这使得上面的代码毫无意义。

如果尚未创建订单,则绝对无法将订单项与订单绑定。至少不是不做其他事情。

这就是我要做的。

  1. 向您的 OrderItem 添加一个新字段:

    order_uuid = models.CharField(max_length=25, blank=False, null=True)
    
  2. 当您开始您的订单流程时(无论您从哪里开始),使用 python 的uuid包创建一个 UUID:

    import uuid
    temporary_order_uuid = uuid.uuid4()
    
  3. 通过您的每个订单步骤传递此值,最终将其保存到orderitem您创建它的时候。

    oi=OrderItem.objects.create(order_uuid=temporary_order_uuid, …)
    
  4. 创建您的 后order,返回并orderitems使用订单的 pk 更新所有包含该 uuid 的内容:

    order_items = OrderItems.objects.get(order_uuid=temporary_order_uuid)
    for oi in order_items:
        oi.order = order
        oi.save()
    
  5. 一个更清洁的解决方案是在您orderitems在数据库中创建订单之后创建您的,但我不知道您的应用程序的所有各种要求。

于 2013-02-27T08:19:54.260 回答