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 7a9ec54 commit 53b53d1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
20 changes: 20 additions & 0 deletions import_export_celery/migrations/0011_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", "0010_auto_20231013_0904"),
]

operations = [
migrations.AlterField(
model_name="exportjob",
name="queryset",
field=models.JSONField(
verbose_name="JSON list of pks to export or dict of queryset filters"
),
),
]
17 changes: 11 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 @@ -67,8 +66,9 @@ def __init__(self, *args, **kwargs):
default="",
)

queryset = models.TextField(
verbose_name=_("JSON list of pks to export"),

queryset = models.JSONField(

Check failure on line 70 in import_export_celery/models/exportjob.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (2)

Check failure on line 70 in import_export_celery/models/exportjob.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (2)
verbose_name=_("JSON list of pks to export or dict of queryset filters"),
null=False,
)

Expand Down Expand Up @@ -104,14 +104,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().get_export_queryset().filter(pk__in=pks)
return self.get_content_type().model_class().objects.filter(pk__in=pks)
return resource_class().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 53b53d1

Please sign in to comment.