简体   繁体   中英

Object of type 'TYPE' is not JSON serializable Django

I'm using ReactJS and Graphql as frontend and django and graphene as backend.

In django I have code as follows:

company = Company.objects.get(pk=input.company.id)   
###### Result is <Company: Company object (14)>        
UserByManagerCreated.delay(company=company)

And in UserByManagerCreated I have :

@task
def UserByManagerCreated(company):
    #Send emails, ....
    pass

But I'm getting an error Object of type 'Company' is not JSON serializable

Any idea?

You can't send it to a celery task because it should be serializable, because the delayed task is stored in a queue ( Redis or Rabbit ) and it should be serializable. So you can pass all serializable types as arguments to a celery task.

What I suggest is to pass those values that you need to use in the task. Not the company itself, but only values you need. Or you can put them into a dictionaty and pass it instead.

   company = Company.objects.get(pk=input.company.id)
   company_data = {'id': company.id, 'your_field': company.your_field}   
   UserByManagerCreated.delay(company=company_data)

And as AKX advised you can pass only id and retrieve your company right in the task. But if it's a sending mail task I think you can send only needed emails list as argument to be sent an email there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM