Skip to content

Commit 53b53d1

Browse files
committed
add possibility to specify queryset by filters
1 parent 7a9ec54 commit 53b53d1

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-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", "0010_auto_20231013_0904"),
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: 11 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

@@ -67,8 +66,9 @@ def __init__(self, *args, **kwargs):
6766
default="",
6867
)
6968

70-
queryset = models.TextField(
71-
verbose_name=_("JSON list of pks to export"),
69+
70+
queryset = models.JSONField(
71+
verbose_name=_("JSON list of pks to export or dict of queryset filters"),
7272
null=False,
7373
)
7474

@@ -104,14 +104,19 @@ def get_content_type(self):
104104
return self._content_type
105105

106106
def get_queryset(self):
107-
pks = json.loads(self.queryset)
107+
queryset_spec = self.queryset
108+
if isinstance(queryset_spec, list):
109+
filters = {"pk__in": queryset_spec}
110+
elif isinstance(queryset_spec, dict):
111+
filters = queryset_spec
112+
108113
# If customised queryset for the model exists
109114
# then it'll apply filter on that otherwise it'll
110115
# apply filter directly on the model.
111116
resource_class = self.get_resource_class()
112117
if hasattr(resource_class, "get_export_queryset"):
113-
return resource_class().get_export_queryset().filter(pk__in=pks)
114-
return self.get_content_type().model_class().objects.filter(pk__in=pks)
118+
return resource_class().get_export_queryset().filter(**filters)
119+
return self.get_content_type().model_class().objects.filter(**filters)
115120

116121
def get_resource_choices(self):
117122
return [

0 commit comments

Comments
 (0)