title: Django to clear data causing save() prohibited to prevent data loss due to unsaved related object
body: <p>I need to know how to clear the unsaved related data which causes the following error:</p>
  
  <p>save() prohibited to prevent data loss due to unsaved related object 'order'.</p>
  
  <p>My code is working as intended, but I failed to complete an order. Ever since this, any attempt to save a new order results in above error. I am aware of the cause being the changes to <code>django.db.models.base.py</code>, but there must be some way to clear this via logs or something...
  I have tried recreating the database, and also the sqlflush command but neither is working.</p>
  
  <p>VIEWS</p>
  
  <pre><code># -*- coding: utf-8 -*-
  from __future__ import unicode_literals
  import weasyprint
  from django.shortcuts import render, redirect, get_object_or_404
  from django.contrib.admin.views.decorators import staff_member_required
  from django.core.urlresolvers import reverse
  from django.conf import settings
  from django.http import HttpResponse
  from django.template.loader import render_to_string
  from .models import OrderItem, Order
  from .forms import OrderCreateForm
  from cart.cart import Cart
  from .tasks import order_created
  # Create your views here.
  @staff_member_required
  def admin_order_detail(request, order_id):
      order = get_object_or_404(Order, id=order_id)
      return render(request, 'admin/orders/order/detail.html', {'order': order})
  
  @staff_member_required
  def admin_order_pdf(request, order_id):
      order = get_object_or_404(Order, id=order_id)
      html = render_to_string('orders/order/pdf.html', {'order': order})
      response = HttpResponse(content_type='application/pdf')
      response['Content-Disposition'] = 'filename="order_{}.pdf"'.format(order.id)
      weasyprint.HTML(string=html).write_pdf(response, stylesheets=[weasyprint.CSS(settings.STATIC_ROOT + 'css/pdf.css')])
  
      return response
  
  
  def order_create(request):
      cart = Cart(request)
      if request.method == 'POST':
          form = OrderCreateForm(request.POST)
          if form.is_valid():
              order = form.save(commit=False)
              if cart.coupon:
                  order.coupon = cart.coupon
                  order.discount = cart.coupon.discount
                  order.save()
              for item in cart:
                  OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity'])
              #emptying the cart
              cart.clear()
              #launch celery async task
              order_created.delay(order.id)
              request.session['order_id'] = order.id #set order.id session
              return redirect(reverse('payment:process'))
      else:
          form = OrderCreateForm()
      return render(request, 'orders/order/create.html', {'cart': cart, 'form':form})
  </code></pre>
  
  <p>MODELS</p>
  
  <pre><code>class Order(models.Model):
      first_name = models.CharField(_('first_name'),max_length=50)
      last_name = models.CharField(_('last_name'),max_length=50)
      email = models.EmailField(_('email'),)
      address = models.CharField(_('address'),max_length=250)
      postal_code = models.CharField(_('postal_code'),max_length=250)
      city = models.CharField(_('city'),max_length=100)
      created = models.DateTimeField(auto_now_add=True)
      updated = models.DateTimeField(auto_now=True)
      paid = models.BooleanField(default=False)
      coupon = models.ForeignKey(Coupon, related_name='orders', null=True, blank=True)
      discount = models.IntegerField(default=0, validators=[MinValueValidator(0), MaxValueValidator(100)])
  
      class Meta:
          ordering = ('-created',)
  
      def __unicode__(self):
          return 'Order {}'.format(self.id)
  
      def get_total_cost(self):
          total_cost  = sum(item.get_cost() for item in self.items.all())
          return total_cost - total_cost *(self.discount)
  
  class OrderItem(models.Model):
      order = models.ForeignKey(Order, related_name='items')
      product = models.ForeignKey(Product, related_name='order_items')
      price = models.DecimalField(max_digits=10, decimal_places=2)
      quantity = models.PositiveIntegerField(default=1)
  
      def __unicode__(self):
          return '{}'.format(self.id)
  
      def get_cost(self):
          return self.price * self.quantity
  </code></pre>
  
  <p>FORMS</p>
  
  <pre><code>from django import forms
  from .models import Order
  
  
  class OrderCreateForm(forms.ModelForm):
      class Meta:
          model = Order
          fields = ['first_name', 'last_name', 'email', 'address', 'postal_code', 'city']
  </code></pre>