Serializer error fixed by print?

24 views
Skip to first unread message

sebastian wrzalek

unread,
May 18, 2020, 4:47:25 PM5/18/20
to Django REST framework
Hello,
I have found very strange behaviour.
In my app I have one serializer for partial update and one for data with nested representations.
This block of code: 
@database_sync_to_async
   def _update_order(self, content):
       instance = Order.objects.get(id=content.get('id'))
       serializer = UpdateOrderSerializer(data=content, partial=True)
       serializer.is_valid(raise_exception=True)
       order = serializer.update(instance, serializer.validated_data)
       #if u remove print u get an error ???
       print(order)
       return order

Couses error

  1. app_1    | Exception inside application: You cannot call this from an async context - use a thread or sync_to_async.
  2. app_1    | Traceback (most recent call last):
  3. app_1    |   File "/usr/local/lib/python3.7/site-packages/django/db/models/fields/related_descriptors.py", line 172, in __get__
  4. app_1    |     rel_obj = self.field.get_cached_value(instance)
  5. app_1    |   File "/usr/local/lib/python3.7/site-packages/django/db/models/fields/mixins.py", line 13, in get_cached_value
  6. app_1    |     return instance._state.fields_cache[cache_name]
  7. app_1    | KeyError: 'premises'

But when I uncomment print(order)
Evertything as it should.
I have no idea why if u know im looking forward to hear.

Full code:

Linovia

unread,
May 18, 2020, 5:48:17 PM5/18/20
to Django REST framework
You probably have a queryset somewhere that is evaluated with the print and isn’t without.
Point is, it may delay synchronous DB requests in an asynchronous code which would raise the error.

Regards,
Xavier O.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/c96b6e42-e6cf-4cf8-8d3f-6ceaa11e145f%40googlegroups.com.

sebastian wrzalek

unread,
May 18, 2020, 6:47:10 PM5/18/20
to Django REST framework
Thanks for answer. When I in request object I send id of premises it works fine. 
Request which led to error contained only id of order and one additional field.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages