Skip to content

Commit

Permalink
Merge pull request #28 from Faisal-Manzer/master
Browse files Browse the repository at this point in the history
Corrected views and README
  • Loading branch information
farhan0581 authored Dec 25, 2019
2 parents 0d3b239 + a2f4515 commit 2f4f80b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ venv.bak/

# mypy
.mypy_cache/

.idea
43 changes: 29 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ Installation:
------------
You can install it via pip or to get the latest version clone this repo.

`
```shell script
pip install django-admin-autocomplete-filter
`
```

Add ``admin_auto_filters`` to your ``INSTALLED_APPS`` inside settings.py of your project.
Add `admin_auto_filters` to your `INSTALLED_APPS` inside settings.py of your project.

Usage:
-----
Let's say we have following models:
``` python
```python
from django.db import models

class Artist(models.Model):
name = models.CharField(max_length=128)

Expand All @@ -41,27 +43,32 @@ class Album(models.Model):
```
And you would like to filter results in Album Admin on the basis of artist, then you can define `search fields` in Artist and then define filter as:

``` python
```python
from django.contrib import admin
from admin_auto_filters.filters import AutocompleteFilter


class ArtistFilter(AutocompleteFilter):
title = 'Artist' # display title
field_name = 'artist' # name of the foreign key field


class ArtistAdmin(admin.ModelAdmin):
search_fields = ['name'] # this is required for django's autocomplete functionality
...
# ...


class AlbumAdmin(admin.ModelAdmin):
list_filter = [ArtistFilter]

'''
defining this class is required for AutocompleteFilter
it's a bug and I am working on it.
'''
"""
defining this class is required for AutocompleteFilter
it's a bug and I am working on it.
"""
class Media:
pass
...

# ...
```

After following these steps you may see the filter as:
Expand All @@ -78,18 +85,22 @@ In your views.py:
```python
from admin_auto_filters.views import AutocompleteJsonView


class CustomSearchView(AutocompleteJsonView):
def get_queryset(self):
'''
your custom logic goes here.
'''
"""
your custom logic goes here.
"""
queryset = Artist.objects.all().order_by('name')
return queryset
```

After this, register this view in your admin class as:

```python
from django.contrib import admin
from django.urls import path


class AlbumAdmin(admin.ModelAdmin):
list_filter = [ArtistFilter]
Expand All @@ -109,6 +120,10 @@ class AlbumAdmin(admin.ModelAdmin):
Finally just tell the filter class to use this new view as:

```python
from django.shortcuts import reverse
from admin_auto_filters.filters import AutocompleteFilter


class ArtistFilter(AutocompleteFilter):
title = 'Artist'
field_name = 'artist'
Expand Down
10 changes: 1 addition & 9 deletions admin_auto_filters/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
from django.shortcuts import render, render_to_response
from django.views.generic import CreateView
from testing.models import *
from testing.forms import *
from django.views import View
from django.http import JsonResponse
import json
import pprint
from django.views.generic.list import BaseListView
from django.contrib.admin.views.autocomplete import AutocompleteJsonView as Base


Expand All @@ -24,4 +16,4 @@ def get(self, request, *args, **kwargs):
for obj in context['object_list']
],
'pagination': {'more': context['page_obj'].has_next()},
})
})
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='django-admin-autocomplete-filter',
version='0.3',
version='0.4',
packages=find_packages(),
include_package_data=True,
description='A simple Django app to render list filters in django admin using autocomplete widget',
Expand Down

0 comments on commit 2f4f80b

Please sign in to comment.