Skip to content

Fix #59 - Add a get_queryset helper to be able to provide a site related queryset. #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions django_select2/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,17 @@ def __init__(self, *args, **kwargs):
value. (Default is ``pk``, i.e. the id field of the model)
:type to_field_name: :py:obj:`str`
"""
if self.queryset is None and not 'queryset' in kwargs:
raise ValueError('queryset is required.')

self.max_results = getattr(self, 'max_results', None)
self.to_field_name = getattr(self, 'to_field_name', 'pk')

super(ModelResultJsonMixin, self).__init__(*args, **kwargs)

def get_queryset(self):
if self.queryset is None:
raise ValueError('queryset is required.')

return self.queryset

def label_from_instance(self, obj):
"""
Sub-classes should override this to generate custom label texts for values.
Expand Down Expand Up @@ -246,7 +249,7 @@ def get_results(self, request, term, page, context):
if not hasattr(self, 'search_fields') or not self.search_fields:
raise ValueError('search_fields is required.')

qs = copy.deepcopy(self.queryset)
qs = copy.deepcopy(self.get_queryset())
params = self.prepare_qs_params(request, term, self.search_fields)

if self.max_results:
Expand Down