2

Im working on a Django project where i was using postgres and it was working fine. Now i want to use MongoDB but im getting "No exception message supplied" error most of times when querying on objects. please find the below codes for reference and help.

Model

class Order(models.Model):
    order_status = (('Pending', 'Pending'), ('Under Process', 'Under Process'), ('Dispatched', 
    'Dispatched'),
                    ('Delivered', 'Delivered'), ('Cancelled', 'Cancelled'), ('Out for 
    delivery', 
   'Out for delivery'))
    patient = models.ForeignKey(Patient, on_delete=models.SET_NULL, null=True, blank=True)
    shipping_address = models.ForeignKey(ShippingAddress, on_delete=models.DO_NOTHING, 
    null=True, 
    blank=True)
    order_created = models.DateTimeField(auto_now_add=True,null=True)
    date_ordered = models.DateTimeField(null=True)
    complete = models.BooleanField(default=False)
    transaction_id = models.CharField(max_length=100, null=True)
    order_number = models.CharField(max_length=20, unique=True, null=True)
    status = models.CharField(choices=order_status, default='Pending', max_length=30)
    payment_status = models.CharField(choices=(('Received', 'Received'), ('Failed', 'Failed'), 
    ('Pending', 'Pending')),
                                      default='Pending', max_length=30)
    ip_address = models.CharField(max_length=30, null=True)
    coupon_applied = models.BooleanField(null=True,blank=True)
    coupon = 
    models.ForeignKey(ShoppingCoupon,on_delete=models.DO_NOTHING,null=True,blank=True)
    coupon_discount = models.DecimalField(max_digits=7,decimal_places=2, 
    null=True,blank=True,default=0)
    # Payment details captured from payment gateway
    payment_order_id = models.CharField(max_length=100, null=True) # razorpay order id
    payment_id = models.CharField(max_length=100, null=True) # Razorpay payment id
    payment_signature = models.CharField(max_length=100, null=True)
    payment_method = models.CharField(max_length=100, null=True)
    ordered_medicine = models.JSONField(null=True,blank=True)

Query

def cartData(request):
if request.user.is_authenticated and request.user.user_type == 'patient':
    patient = Patient.objects.select_related('user').get(user=request.user)
    order, created = Order.objects.get_or_create(patient=patient, complete=False) // Im 
    getting error here
    items = order.cartitem_set.select_related('medicine').all()
    cartItems = order.get_cart_items

im getting error on "order, created = Order.objects.get_or_create(patient=patient, complete=False)"

3
  • You can use try except bloc to achieve this goal too : try: order = Order.objects.get(patient=patient, complete=False) except: Order.DoesNotExist as e: print(e.message) order = Order(patient=patient, complete=False) order.save() Commented Sep 2, 2021 at 10:27
  • Thanks for your reply. but this is not working Commented Sep 3, 2021 at 5:52
  • What is the error type. ValueError ? TypeError ? try to catch it in the except block too Commented Sep 3, 2021 at 12:44

2 Answers 2

1

I have the same problem got_or_create method doesn't work with django/mongo. especially BooleanField. If I delete BooleanField its working well.

Sign up to request clarification or add additional context in comments.

Comments

0

Django's Object Relational Mapping is mostly designed for relational databases like PostgreSQL or MySQL, and MongoDB is a NoSQL. For MongoDB with Django, there are packages like like "djongo" or "django-mongodb-engine." These provide compatibility layers needed to access the underlying DB. I was thinking that if you intend to use MongoDB as the primary database, it might be more appropriate to use an ORM specifically designed for MongoDB, such as MongoEngine or PyMODM. These ORMs provide more resilience with a document-based structures and its query language. There may be no direct equivalent of get_or_create() so this may be a similar logic but for MongoDB:

from django.core.exceptions import ObjectDoesNotExist

try:
    order = Order.objects.get(patient=patient, complete=False)
except ObjectDoesNotExist as e:
    print(str(e))
    order = Order(patient=patient, complete=False)
    order.save()
except Exception as e:
    print(str(e))

You may be switching code packages already, so in that case, this may only work for MongoDB and Django ORM packages.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.