简体   繁体   English

“TYPE”类型的对象不是JSON可序列化的Django

[英]Object of type 'TYPE' is not JSON serializable Django

I'm using ReactJS and Graphql as frontend and django and graphene as backend. 我使用ReactJS和Graphql作为前端,使用django和graphene作为后端。

In django I have code as follows: 在django我有如下代码:

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

And in UserByManagerCreated I have : UserByManagerCreated我有:

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

But I'm getting an error Object of type 'Company' is not JSON serializable 但是我收到错误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. 您不能将它发送到芹菜任务,因为它应该是可序列化的,因为延迟的任务存储在队列( RedisRabbit )中,并且它应该是可序列化的。 So you can pass all serializable types as arguments to a celery task. 因此,您可以将所有可序列化类型作为参数传递给celery任务。

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. 不是company本身,而只是您需要的价值观。 Or you can put them into a dictionaty and pass it instead. 或者你可以将它们放入dictionaty并传递它。

   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. 正如AKX建议您只能传递ID并在任务中检索您的company 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. 但是,如果它是发送邮件任务,我认为您只能发送所需的电子邮件列表作为参数,以便在那里发送电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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