Skip to content

Commit ae1244e

Browse files
committed
add possibility to specify queryset by filters
1 parent 57d59e9 commit ae1244e

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Generated by Django 5.0.7 on 2024-07-29 12:21
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("import_export_celery", "0011_exportjob_resource_kwargs"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="exportjob",
15+
name="queryset",
16+
field=models.JSONField(
17+
verbose_name="JSON list of pks to export or dict of queryset filters"
18+
),
19+
),
20+
]

import_export_celery/models/exportjob.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Copyright (C) 2019 o.s. Auto*Mat
22
from django.utils import timezone
3-
import json
43

54
from author.decorators import with_author
65

@@ -74,8 +73,8 @@ def __init__(self, *args, **kwargs):
7473
blank=True,
7574
)
7675

77-
queryset = models.TextField(
78-
verbose_name=_("JSON list of pks to export"),
76+
queryset = models.JSONField(
77+
verbose_name=_("JSON list of pks to export or dict of queryset filters"),
7978
null=False,
8079
)
8180

@@ -111,14 +110,19 @@ def get_content_type(self):
111110
return self._content_type
112111

113112
def get_queryset(self):
114-
pks = json.loads(self.queryset)
113+
queryset_spec = self.queryset
114+
if isinstance(queryset_spec, list):
115+
filters = {"pk__in": queryset_spec}
116+
elif isinstance(queryset_spec, dict):
117+
filters = queryset_spec
118+
115119
# If customised queryset for the model exists
116120
# then it'll apply filter on that otherwise it'll
117121
# apply filter directly on the model.
118122
resource_class = self.get_resource_class()
119123
if hasattr(resource_class, "get_export_queryset"):
120-
return resource_class(**self.resource_kwargs).get_export_queryset().filter(pk__in=pks)
121-
return self.get_content_type().model_class().objects.filter(pk__in=pks)
124+
return resource_class(**self.resource_kwargs).get_export_queryset().filter(**filters)
125+
return self.get_content_type().model_class().objects.filter(**filters)
122126

123127
def get_resource_choices(self):
124128
return [

0 commit comments

Comments
 (0)