2012-08-17 38 views
0

(編輯添加了更多代碼)將綁定方法轉換爲十進制

在它要求我將綁定方法轉換爲十進制時,請在這裏繼續收到錯誤。我認爲這個問題是這一行:

order_item = OrderItem(item=item.item_id, quantity=item.quantity, price=item.price, order=order) 

我認爲Django的想讓我item.price轉換爲十進制,但我一直無法弄清楚如何。我嘗試了decimal.Decimal(str(item.price)),它沒有工作,float(item.price)也沒有工作。一如既往,先謝謝你。

#cart.get_cart_items 
def get_cart_items(request): 
    return Cart.objects.filter(cart_id=_cart_id(request)) 

#models 
class Cart(models.Model): 
    cart_id = models.CharField(max_length=50) 
    item_id = models.ForeignKey('store.Item', unique=False) 
    date_added = models.DateTimeField(auto_now_add=True) 
    quantity = models.IntegerField(default=1) 

class Item(models.Model): 
    user = models.ForeignKey(User) 
    name = models.CharField(max_length=75) 
    slug = models.SlugField(max_length=50, unique=True) 
    is_active = models.BooleanField(default=True, blank=True) 
    image1 = models.ImageField(upload_to='img') 
    image2 = models.ImageField(upload_to='img', blank=True) 
    image3 = models.ImageField(upload_to='img', blank=True) 
    image_caption1 = models.CharField(max_length=200, blank=True) 
    image_caption2 = models.CharField(max_length=200, blank=True) 
    image_caption3 = models.CharField(max_length=200, blank=True) 
    price = models.DecimalField(max_digits=8, decimal_places=2) 
    quantity = models.IntegerField(default=1) 
    description = models.TextField() 
    created = models.DateTimeField(auto_now_add=True) 
    shipping_price = models.DecimalField(decimal_places=2, max_digits=6) 
    categories = models.ManyToManyField(Category) 



#views  
    def express_payment(request): 
      user = request.user 
      cart_subtotal = cart.cart_subtotal(request) 

      if request.method == "POST": 
       form = PaymentForm(request.POST) 
       if form.is_valid(): 
        order = form.save(commit=False) 
        order.buyer = request.user 
        order.transaction_id = "123456789" 
        order.save() 
        if order.pk: 
         cart_items = cart.get_cart_items(request) 
         for item in cart_items: 
          order_item = OrderItem(item=item.item_id, quantity=item.quantity, price=item.price, order=order) 
          order_item.save() 
+1

'item' is .....? – 2012-08-17 03:48:40

回答

0

請提供詳細的錯誤信息,或我們不能提供任何意義幫助..

編輯:

假設class ItemOrderItem是同樣的事情,那麼我認爲username ,需要保存OrderItem實例。

+0

對不起,添加了意見和模型 – Jonathan 2012-08-17 04:26:28

相關問題