summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
Diffstat (limited to 'django')
-rw-r--r--django/tasks/base.py19
1 files changed, 18 insertions, 1 deletions
diff --git a/django/tasks/base.py b/django/tasks/base.py
index 94eb29f2ea..b5c960a7cf 100644
--- a/django/tasks/base.py
+++ b/django/tasks/base.py
@@ -1,5 +1,5 @@
from collections.abc import Callable
-from dataclasses import dataclass, field, replace
+from dataclasses import dataclass, field, fields, replace
from datetime import datetime
from inspect import isclass, iscoroutinefunction
from typing import Any
@@ -55,6 +55,23 @@ class Task:
def __post_init__(self):
self.get_backend().validate_task(self)
+ @classmethod
+ def _reconstruct(cls, kwargs):
+ func_path = kwargs["func"]
+ try:
+ func = import_string(func_path)
+ kwargs["func"] = func.func
+ except (ImportError, AttributeError) as e:
+ msg = f"Expected {func_path!r} to point to a Task instance."
+ raise ValueError(msg) from e
+ return cls(**kwargs)
+
+ def __reduce__(self):
+ kwargs = {f.name: getattr(self, f.name) for f in fields(self)}
+ kwargs["func"] = self.module_path
+
+ return (self.__class__._reconstruct, (kwargs,))
+
@property
def name(self):
return self.func.__name__