title: How to copy an existing django object to another table for temporary storage?
body: <p>Basically I have an inventory system and a POS, cart and checkout.</p>
  <p>Here are my models:</p>
  <pre><code>class OrderItem(models.Model):
      customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
      ordered = models.BooleanField(default=False)
      item = models.ForeignKey(Item, on_delete=models.CASCADE)
      qty = models.IntegerField(default=1)
      date_created = models.DateTimeField(auto_now_add=False, auto_now=True, blank=True, null=True)
  
      def __str__(self):
          return f&quot;{self.item.upc} || ${self.item.sug_ind_retail} || ({self.item.item_name}) X {self.qty} &quot;
  
      def get_total_item_price(self):
          return self.qty * self.item.sug_ind_retail
  
      def get_item_sales_tax(self):
          return self.get_total_item_price() * .095
  
      def get_total(self):
          return self.get_item_sales_tax() + (self.item.sug_ind_retail * self.qty)
  
  
  class TempCartItem(models.Model):
      customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
      ordered = models.BooleanField(default=False)
      item = models.ForeignKey(Item, on_delete=models.CASCADE)
      qty = models.IntegerField(default=1)
      date_created = models.DateTimeField(auto_now_add=False, auto_now=True, blank=True, null=True)
  
      def __str__(self):
          return f&quot;{self.item.upc} || ${self.item.sug_ind_retail} || ({self.item.item_name}) X {self.qty} &quot;
  
      def get_total_item_price(self):
          return self.qty * self.item.sug_ind_retail
  
      def get_item_sales_tax(self):
          return self.get_total_item_price() * .095
  
      def get_total(self):
          return self.get_item_sales_tax() + (self.item.sug_ind_retail * self.qty)
  </code></pre>
  <p>Basically I want to save the TempCartItem's into the OrderItem model, this way I can delete all the items in the cart but save the OrderItem's when we check out a customer.</p>
  <p>Here is my view:</p>
  <pre><code>@login_required
  def order_item_view(request):
      temp_order_item_form = TempCartItemForm(request.POST or None)
      
          if request.POST.get(&quot;submit&quot;):
          temp_order_item_form.save(commit=False)
          form_upc = temp_order_item_form.cleaned_data.get('upc')
          if form_upc == '':
              messages.warning(request, &quot;UPC is required to add to cart.&quot;)
          form_qty = temp_order_item_form.cleaned_data.get('qty')
          global customer
          customer = temp_order_item_form.cleaned_data.get('customer')
  
          # get item or create new one if it doesn't exist
          try:
              item = Item.objects.get(upc=form_upc, amz_truck_id=1593)
  
              temp_order_item_obj = TempCartItem(customer=customer, ordered=False, item=item, qty=form_qty)
              temp_order_item_obj.save()
              messages.success(request, 'Item successfully added to cart')
          except Exception:
              messages.warning(request, 'No item found')
  
          queryset = TempCartItem.objects.filter(customer=customer)
  
          context = {
              &quot;order_item_form&quot;: temp_order_item_form,
              &quot;queryset&quot;: queryset,
          }
  
          return render(request, &quot;cart.html&quot;, context)
  
      if request.POST.get(&quot;view_cart&quot;):
          temp_order_item_form.save(commit=False)
          customer = temp_order_item_form.cleaned_data.get('customer')
          queryset = TempCartItem.objects.filter(customer=customer)
          context = {
              &quot;order_item_form&quot;: temp_order_item_form,
              &quot;queryset&quot;: queryset,
          }
  
          return render(request, &quot;cart.html&quot;, context)
  
      context = {
          &quot;order_item_form&quot;: temp_order_item_form,
      }
  
      if request.POST.get(&quot;checkout&quot;):
          temp_order_item_form.save(commit=False)
  
          customer = temp_order_item_form.cleaned_data.get('customer')
          ref_code = create_ref_code()
  
          items = TempCartItem.objects.filter(customer=customer)
  
          for item in items:
              order_item = OrderItem.objects.create(item)
              order_item.pk = None
              order_item.save()
  
          instance = Order.objects.create(customer=customer, ref_code=ref_code)
  
          for item in items:
              instance.item.add(item)
  
  
          instance.save()
          messages.success(request, 'Order successfully created')
  
  
          queryset = Order.objects.all()
  
          context = {
              &quot;queryset&quot;: queryset,
          }
  
          return render(request, &quot;orders.html&quot;, context)
  
      return render(request, &quot;cart.html&quot;, context)
  </code></pre>
  <p>In my &quot;checkout&quot; part of the view, how can I save all the TempCartItem's into the OrderItem's table? This way I can delete all the TempCartItem's after the order is processed so the cart is cleared but the OrderItem's are still saved.</p>
  <p>I tried</p>
  <pre><code>      items = TempCartItem.objects.filter(customer=customer)
        for item in items:
              order_item = OrderItem.objects.create(item)
              order_item.pk = None
              order_item.save()
  </code></pre>
  <p>Any suggestions?</p>
  <p>Thanks in advance!</p>