I am developing my app with Djnago and for database mongo db . Its basically a subscription buying platform. On 2nd time buying a subscription Its throwing error .
In order model I have:
class Order(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
ref_code = models.CharField(max_length=20, blank=True, null=True)
subscriptions = models.ManyToManyField(OrderSubscription)
start_date = models.DateTimeField(auto_now_add=True)
ordered_date = models.DateTimeField()
ordered = models.BooleanField(default=False)
amount = models.FloatField()
billing_address = models.ForeignKey(
'Address', related_name='billing_address', on_delete=models.SET_NULL, blank=True, null=True)
payment = models.ForeignKey(
'Payment', on_delete=models.SET_NULL, blank=True, null=True)
coupon = models.ForeignKey(
'Coupon', on_delete=models.SET_NULL, blank=True, null=True)
being_delivered = models.BooleanField(default=False)
received = models.BooleanField(default=False)
refund_requested = models.BooleanField(default=False)
refund_granted = models.BooleanField(default=False)
When someone brought a subscription and want to buy again another Sub Its throwing duplicate key error where Its creating another order table with the same user.
def finalcheckout(request):
if request.method == "POST":
subscription = get_object_or_404(Subscription, slug = request.POST.get("sub_slug"))
order_subscription, created = OrderSubscription.objects.get_or_create(
subscription=subscription,
user=request.user,
ordered=False
)
order = Order.objects.create(user=request.user)
order.subscriptions.add(order_subscription)
messages.info(request, "This subscription was added to your cart.")
return redirect("core:order-summary")
Error:
Params: [2, None, datetime.datetime(2020, 3, 2, 12, 26, 53, 481937), None, False, None, None, None, None, False, False, False, False] Pymongo error: {'writeErrors': [{'index': 0, 'code': 11000, 'keyPattern': {'backup_id': 1}, 'keyValue': {'backup_id': None}, 'errmsg': 'E11000 duplicate key error collection: hostmanager.subscription_order index: subscription_order_backup_id_79d0cc4e_uniq dup key: { backup_id: null }',