Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy would django give me an ObjectDoesNotExist error on a newly created object?
    primarykey
    data
    text
    <p>I just ran into this while testing some code that uses django to handle orders. I'm getting an "ObjectDoesNotExist" error when trying to load (by id) a newly created record.</p> <p>Here's a skeleton of how it works:</p> <ul> <li>Someone places an order, and its details get saved in a record on a table called "trader_order".</li> <li>The values in that object are used in creating records in another table (which aren't relevant to the problem here, outside of the fact that they successfully use the ID of the new order).</li> <li>The ID of the order record is then pushed into a FIFO buffer.</li> <li>The function that did all of this then sends out an e-mail, and ends.</li> </ul> <p>The FIFO buffer is being read by another django script, which is waiting for an ID to be passed into it (running in the background as a daemon). Once it receives that id, it tries to load the order record with that id, and process it.</p> <p>This has been working well (in testing anyway), until one instance today. I placed a test order (nothing special about it), and got an error saying that the record does not exist. I'm quite baffled as to why. Here's the code that's watching the buffer:</p> <pre><code>def handle(self, *args, **options): ### # code chopped out for brevity. Just opening (successfully) the fifo buffer ### done = 0 str = '' while not done: chr = fifo.read(1); if(len(chr)): print "read %s" % chr if(chr == ','): str = str.strip() if(str == 'quit'): done = 1 elif(str.isdigit()): print "We have a valid id: %s" % str self.process_order(str) str = '' else: str += chr else: time.sleep(1) fifo.close() def process_order(self, order_id): try: order = models.Order.objects.get(id=order_id) print "Found Order:" print order except ObjectDoesNotExist: print "Order with id %s does not exist"%order_id return # code continues here, but is not relevant to the question </code></pre> <p>And here is the output that it gave on the console:</p> <pre><code>read 1 read 2 read 3 read 1 read 6 read 7 read , We have a valid id: 123167 Order with id 123167 does not exist </code></pre> <p>When I looked at the database, that ID does indeed exist. I'm at a loss as to why it gave me that error. I have not been able to reproduce it either. When I passed that same id into the buffer manually, it processed the order perfectly. When I tried to reproduce the circumstances, it again worked perfectly.</p> <p>Could this somehow be due to the fact that these two bits of code are separately connected to the MySQL server? Perhaps something needs to be flushed before the listening script tries to read the record from the database?</p> <p>A couple of relevant bits of info here:</p> <ul> <li>I'm running the page in a test environment with django's manage.py ("./manage.py runserver 0.0.0.0:8000")</li> <li>The code above is also running as a background task with manage.py ("./manage.py cronprocessorders")</li> <li>The database is in MySQL</li> <li>Running on an Ubuntu distro</li> </ul> <h2>Update</h2> <p>As per requests made, here's more code. This is the function that actually places the order and and puts its ID into the buffer that's being read by the code above:</p> <pre><code>def order_place(self, order): '''Place and order on the system. The system will deduct the fee, escrow the amount and create an unprocessed open order. ''' balance = self.balance() for cur, bal in balance.items(): limit = self.limit(cur, 'balance') if limit != None and bal &gt; limit: raise BalanceLimit(limit, cur) if order.order_total() &gt; balance[order.currency_from()]: raise NotSufficientFunds order.profile = self # Set up the fee for the order tr_fee = Transaction() tr_fee.profile = order.profile tr_fee.reason = 'fee' tr_fee.currency = order.currency_from() tr_fee.amount = -order.fee() tr_fee.notes = "%.2f%%" % (order.feerate()) order.save() # Commit the fee now that the order is saved if tr_fee.amount &lt; 0: tr_fee.processed = datetime.datetime.now() tr_fee.order = order tr_fee.save() if order.remaining &gt; 0: # Put the remaining into escrow (debit) tr_da = Transaction() tr_da.profile = order.profile tr_da.processed = datetime.datetime.now() tr_da.reason = 'escrow' tr_da.order = order tr_da.currency = order.currency_from() tr_da.amount = -order.escrow() tr_da.save() #write the order id to the FIFO process for order filling try: fifo = open(settings.FIFO_PROCESS_ORDER_FILE_PATH, 'w+') fifo.write('%s,' % order.id) fifo.close() except IOError: print "file doesn't exist" </code></pre> <p>And that's it. I see in retrospect that I was looking at the wrong function when describing this (sorry). The only difference is the e-mail sent at the end. This one does not do so. The "order" object being passed in is form data that's already scrubbed.</p> <p>Oh, and my apologies for the craptastic variable names. I didn't write this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload