Skip to content

Commit

Permalink
Change get_list_chart_queryset arg to changelist (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert committed May 5, 2022
1 parent d10a12c commit 3f1d46c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Or by using the class methods which gives you access to the queryset being used

```python
class MyModelAdmin(AdminChartMixin, admin.ModelAdmin):
def get_list_chart_queryset(self, result_list):
def get_list_chart_queryset(self, changelist):
...

def get_list_chart_type(self, queryset):
Expand All @@ -104,4 +104,19 @@ The `type`, `data`, and `options` are passed directly to Chart.js to render the
By default, the objects in your chart will be the objects that are currently visible in your list view.
This means that admin controls like [search](https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.search_fields) and [list filter](https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter) will update your chart,
and you can use the Django [pagination](https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_per_page) [settings](https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_max_show_all) to control how many objects you want in your chart at a time.
If you want, you can also sidestep the list queryset entirely by using overriding `get_list_chart_queryset`.
To ignore pagination but still respect search/filter,
you can override the `get_list_chart_queryset` method to return the full queryset:

```python
class MyModelAdmin(AdminChartMixin, admin.ModelAdmin):
def get_list_chart_queryset(self, changelist):
return changelist.queryset
```

And if you want, you can also sidestep the list queryset entirely by using overriding `get_list_chart_queryset` with your own query:

```python
class MyModelAdmin(AdminChartMixin, admin.ModelAdmin):
def get_list_chart_queryset(self, changelist):
return MyModel.objects.all()
```
12 changes: 8 additions & 4 deletions admincharts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ class Media:
"admincharts/admincharts.js",
)

def get_list_chart_queryset(self, result_list):
"""Returns the current changelist results by default"""
return result_list
def get_list_chart_queryset(self, changelist):
"""
Returns the current changelist.result_list by default which
is the filtered *and* paginated queryset.
To ignore pagination, you can return the changelist.queryset instead.
"""
return changelist.result_list

def get_list_chart_type(self, queryset):
return self.list_chart_type
Expand Down Expand Up @@ -48,7 +52,7 @@ def changelist_view(self, request, extra_context=None):

if "cl" in response.context_data:
changelist = response.context_data["cl"]
chart_queryset = self.get_list_chart_queryset(changelist.result_list)
chart_queryset = self.get_list_chart_queryset(changelist)
response.context_data["adminchart_queryset"] = chart_queryset
response.context_data[
"adminchart_chartjs_config"
Expand Down
14 changes: 12 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
from accounts.models import Account


class MockChangelist:
def __init__(self, queryset):
self.queryset = queryset
self.result_list = queryset # Would be the paginated list of objects


class AdminTest(TestCase):
def setUp(self):
Account.objects.create(
Expand All @@ -23,7 +29,9 @@ def setUp(self):
self.model_admin = AccountAdmin(model=Account, admin_site=AdminSite())

def test_admin(self):
queryset = self.model_admin.get_list_chart_queryset(Account.objects.all())
queryset = self.model_admin.get_list_chart_queryset(
MockChangelist(Account.objects.all())
)
self.assertEquals(self.model_admin.get_list_chart_type(queryset), "bar")
chart_data = self.model_admin.get_list_chart_data(queryset)
self.assertIn("labels", chart_data)
Expand All @@ -39,7 +47,9 @@ def setUp(self):
self.model_admin = AccountAdmin(model=Account, admin_site=AdminSite())

def test_admin(self):
queryset = self.model_admin.get_list_chart_queryset(Account.objects.all())
queryset = self.model_admin.get_list_chart_queryset(
MockChangelist(Account.objects.all())
)
self.assertEquals(self.model_admin.get_list_chart_type(queryset), "bar")
self.assertEquals(self.model_admin.get_list_chart_data(queryset), {})
self.assertEquals(
Expand Down

0 comments on commit 3f1d46c

Please sign in to comment.