我可以帮助我如何在模型中保存外键吗?

时间:2013-02-27 08:07:00

标签: django foreign-keys

我有两个模型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之后调用的函数,即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))

我应该指出OrderItemOrder之前被调用..大部分混淆来自于 按照django文档https://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward

的建议,返回与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()
[]

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

1 个答案:

答案 0 :(得分: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), …)

根据您下面的评论,您在创建order之后创建了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后,请返回并使用订单的pk更新包含该uuid的所有orderitems

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