Skip to content

Commit

Permalink
add possibility to specify queryset by filters
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Jul 29, 2024
1 parent 57d59e9 commit ae1244e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
20 changes: 20 additions & 0 deletions import_export_celery/migrations/0012_alter_exportjob_queryset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 5.0.7 on 2024-07-29 12:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("import_export_celery", "0011_exportjob_resource_kwargs"),
]

operations = [
migrations.AlterField(
model_name="exportjob",
name="queryset",
field=models.JSONField(
verbose_name="JSON list of pks to export or dict of queryset filters"
),
),
]
16 changes: 10 additions & 6 deletions import_export_celery/models/exportjob.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (C) 2019 o.s. Auto*Mat
from django.utils import timezone
import json

from author.decorators import with_author

Expand Down Expand Up @@ -74,8 +73,8 @@ def __init__(self, *args, **kwargs):
blank=True,
)

queryset = models.TextField(
verbose_name=_("JSON list of pks to export"),
queryset = models.JSONField(
verbose_name=_("JSON list of pks to export or dict of queryset filters"),
null=False,
)

Expand Down Expand Up @@ -111,14 +110,19 @@ def get_content_type(self):
return self._content_type

def get_queryset(self):
pks = json.loads(self.queryset)
queryset_spec = self.queryset
if isinstance(queryset_spec, list):
filters = {"pk__in": queryset_spec}
elif isinstance(queryset_spec, dict):
filters = queryset_spec

# If customised queryset for the model exists
# then it'll apply filter on that otherwise it'll
# apply filter directly on the model.
resource_class = self.get_resource_class()
if hasattr(resource_class, "get_export_queryset"):
return resource_class(**self.resource_kwargs).get_export_queryset().filter(pk__in=pks)
return self.get_content_type().model_class().objects.filter(pk__in=pks)
return resource_class(**self.resource_kwargs).get_export_queryset().filter(**filters)
return self.get_content_type().model_class().objects.filter(**filters)

def get_resource_choices(self):
return [
Expand Down

0 comments on commit ae1244e

Please sign in to comment.