Skip to content

Commit

Permalink
custom queryset for list view
Browse files Browse the repository at this point in the history
  • Loading branch information
asifpy committed Feb 28, 2016
1 parent 1e1482d commit 216ce5b
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Features:
- post_create and post_update signals to handle specific actions in Create and Update views
- Add your own custom templates for List/Create/Detail/Update/Delete views
- Separate CREATE and UPDATE forms
- Define your own custom queryset for list view

Prerequisites
-------------
Expand Down
1 change: 1 addition & 0 deletions crudbuilder/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(
self.login_required = self._has_crud_attr('login_required')

self.custom_templates = self._has_crud_attr('custom_templates')
self.custom_queryset = self._has_crud_attr('custom_queryset')

@property
def get_model_class(self):
Expand Down
5 changes: 4 additions & 1 deletion crudbuilder/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ class BaseListViewMixin(CrudBuilderMixin):
- Override get_queryset method of ListView
"""
def get_queryset(self):
objects = self.model.objects.all()
if self.custom_queryset:
objects = self.custom_queryset(self.request, **self.kwargs)
else:
objects = self.model.objects.all()
search = self.request.GET.get('search')
if search:
q_list = [
Expand Down
6 changes: 6 additions & 0 deletions crudbuilder/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,9 @@ def test_separate_createupdateform(self):

response = self.client.post('/crud/tests/testmodels/1/update/', data)
self.assertEqual(response.status_code, 302)

def test_custom_queryset(self):
def custom_queryset(self, request, **kwargs):
return self.model.objects.all()
setattr(TestModelCrud, 'custom_queryset', custom_queryset)
self.get_list_view()
3 changes: 2 additions & 1 deletion crudbuilder/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def generate_list_view(self):
permissions=self.view_permission('list'),
permission_required=self.check_permission_required,
login_required=self.check_login_required,
table_pagination=self.tables2_pagination or 10
table_pagination=self.tables2_pagination or 10,
custom_queryset=self.custom_queryset
)

list_class = type(
Expand Down
6 changes: 6 additions & 0 deletions docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ Then create the CRUD class for ``Person`` model::
login_required=True
permission_required=True

def custom_queryset(self, request, **kwargs):
"""Define your own custom queryset for list view"""
qset = self.model.objects.filter(created_by=request.user)
return qset

# permissions = {
# 'list': 'example.person_list',
# 'create': 'example.person_create'
Expand Down Expand Up @@ -92,6 +97,7 @@ CRUD class Attributes
- **permission_required** -- Enable permission required for specific model CRUD (by default False)
- **permissions** -- By default crudbuilder will generate crud permissions, if you want to define your own permissions then add permissions dictionary on the CRUD class. For more details on permission, you can check :doc:`custom permission </settings>`
- **createupdate_forms** -- Define separate CREATE and UPDATE forms
- **custom_queryset** -- Define your own custom queryset for list view

Usage of all these attributes you can view in `CRUD class of example project`_ on Github.

Expand Down
2 changes: 2 additions & 0 deletions docs/source/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Features
- All the generated views/tables/forms/url are extendable.
- post_create and post_update signals to handle specific actions in Create and Update views
- Add your own templates for List/Create/Detail/Update/Delete views
- Separate CREATE and UPDATE forms
- Define your own custom queryset for list view


Requirements and Compatibility
Expand Down
7 changes: 6 additions & 1 deletion example/example/crud.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from crudbuilder.abstract import BaseCrudBuilder
from example.models import Person, PersonEmployment
from example.forms import(
PersonEmploymentForm,
PersonEmployementCreateForm,
PersonEmployementUpdateForm
)
Expand All @@ -26,6 +25,9 @@ class PersonCrud(BaseCrudBuilder):
# 'create': 'example.person_create'
# }

def custom_queryset(self, request, **kwargs):
return self.model.objects.all()


class PersonEmploymentCrud(BaseCrudBuilder):
model = PersonEmployment
Expand All @@ -38,3 +40,6 @@ class PersonEmploymentCrud(BaseCrudBuilder):
'create': PersonEmployementCreateForm,
'update': PersonEmployementUpdateForm
}

def custom_queryset(self, request, **kwargs):
return self.model.objects.filter(medical_allowance=False)

0 comments on commit 216ce5b

Please sign in to comment.