how to collect data back to request class in celery Django
i have create function in views.py and in function serializing objects to json after that passing this data to celery task but i have problems with collect data back to request class. That is, I can't collect the data back in json to request class, Because I have to pass this request class to another function by celery task
views.py
def create(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data)
serializer.is_valid(raise_exception=True)
request_data = {
"crm_id": request.JWT.crm_id,
"crm": None,
"crmuser": request.JWT.crmuser.id,
"crm_users": None
}
matching_fields = serializer.validated_data['matching_fields']
import_data.delay(request_data, instance.id, matching_fields)
task.py
@shared_task
def import_data(request_data, instance_id, matching_fields):
instance = ImportRecord.objects.get(id=instance_id)
request_data['crm'] = Crm.objects.get(id=request_data['crm_id'])
request_data['crmuser'] = CrmUser.objects.get(id=request_data['crmuser'])
request_data['crm_users'] = CrmUser.objects.all().filter(crm_id=request_data['crm_id'])
helper = ImportData(matching_fields, instance, request)
helper.import_data()
how can i back to collect json request_data to request class
Solution 1:
Celery is an asynchronous process so as far as I know if you need the result of your celery to use in some other part of your code the best practice is to save the result in your database and then fetch the data where ever you need to.