Skip to content

Commit e391b78

Browse files
committed
Merge branch 'release/1.7.0'
2 parents 053b40c + 54b5115 commit e391b78

24 files changed

+141
-169
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ sudo: false
33
env:
44
- TOX_ENV=py27-flake8
55
- TOX_ENV=py34-flake8
6-
- TOX_ENV=py27-dj17
76
- TOX_ENV=py27-dj18
87
- TOX_ENV=py27-dj19
98
- TOX_ENV=py27-dj110
109
- TOX_ENV=py27-dj111
11-
- TOX_ENV=py34-dj17
1210
- TOX_ENV=py34-dj18
1311
- TOX_ENV=py34-dj19
1412
- TOX_ENV=py34-dj110
1513
- TOX_ENV=py34-dj111
14+
- TOX_ENV=py34-dj20
1615
install:
1716
- pip install -r requirements-test.txt
1817
script:

README.md

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
Edit `ForeignKey`, `ManyToManyField` and `CharField` in Django Admin using jQuery UI AutoComplete.
1+
# Edit `ForeignKey`, `ManyToManyField` and `CharField` in Django Admin using jQuery UI AutoComplete.
32

43
[![Build Status](https://travis-ci.org/crucialfelix/django-ajax-selects.svg?branch=master)](https://travis-ci.org/crucialfelix/django-ajax-selects) [![PyPI version](https://badge.fury.io/py/django-ajax-selects.svg)](https://badge.fury.io/py/django-ajax-selects)
54

@@ -9,16 +8,11 @@ Edit `ForeignKey`, `ManyToManyField` and `CharField` in Django Admin using jQuer
98

109
![selected](/docs/source/_static/kiss-all.png?raw=true)
1110

12-
13-
Documentation
14-
------------------
11+
## Documentation
1512

1613
http://django-ajax-selects.readthedocs.org/en/latest/
1714

18-
19-
20-
Quick Usage
21-
-----------
15+
## Quick Usage
2216

2317
Define a lookup channel:
2418

@@ -43,6 +37,8 @@ Add field to a form:
4337

4438
```python
4539
# yourapp/forms.py
40+
from ajax_select.fields import AutoCompleteSelectMultipleField
41+
4642
class DocumentForm(ModelForm):
4743

4844
class Meta:
@@ -51,46 +47,37 @@ class DocumentForm(ModelForm):
5147
tags = AutoCompleteSelectMultipleField('tags')
5248
```
5349

50+
## Fully customizable
5451

52+
* Customize search query
53+
* Query other resources besides Django ORM
54+
* Format results with HTML
55+
* Customize styling
56+
* Customize security policy
57+
* Add additional custom UI alongside widget
58+
* Integrate with other UI elements elsewhere on the page using the javascript API
59+
* Works in Admin as well as in normal views
5560

56-
Fully customizable
57-
------------------
58-
59-
- Customize search query
60-
- Query other resources besides Django ORM
61-
- Format results with HTML
62-
- Customize styling
63-
- Customize security policy
64-
- Add additional custom UI alongside widget
65-
- Integrate with other UI elements elsewhere on the page using the javascript API
66-
- Works in Admin as well as in normal views
61+
## Assets included by default
6762

63+
* //ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
64+
* //code.jquery.com/ui/1.10.3/jquery-ui.js
65+
* //code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css
6866

69-
Assets included by default
70-
-------------------
67+
## Compatibility
7168

72-
- //ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
73-
- //code.jquery.com/ui/1.10.3/jquery-ui.js
74-
- //code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css
69+
* Django >=1.8, <=2.1
70+
* Python >=2.7, 3.3+
7571

76-
Compatibility
77-
-------------
78-
79-
- Django >=1.6, <=1.10
80-
- Python >=2.7, 3.3-3.5
81-
82-
83-
Contributors
84-
------------
72+
## Contributors
8573

8674
Many thanks to all contributors and pull requesters !
8775

8876
https://github.com/crucialfelix/django-ajax-selects/graphs/contributors
8977

90-
91-
License
92-
-------
78+
## License
9379

9480
Dual licensed under the MIT and GPL licenses:
95-
- http://www.opensource.org/licenses/mit-license.php
96-
- http://www.gnu.org/licenses/gpl.html
81+
82+
* http://www.opensource.org/licenses/mit-license.php
83+
* http://www.gnu.org/licenses/gpl.html

ajax_select/__init__.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""JQuery-Ajax Autocomplete fields for Django Forms."""
2-
__version__ = "1.6.1"
2+
__version__ = "1.7.0"
33
__author__ = "crucialfelix"
44
__contact__ = "[email protected]"
55
__homepage__ = "https://github.com/crucialfelix/django-ajax-selects/"
@@ -8,14 +8,9 @@
88
from ajax_select.helpers import make_ajax_form, make_ajax_field # noqa
99
from ajax_select.lookup_channel import LookupChannel # noqa
1010

11-
try:
12-
# django 1.7+ will use the new AppConfig api
13-
# It will load all your lookups.py modules
14-
# and any specified in settings.AJAX_LOOKUP_CHANNELS
15-
# It will do this after all apps are imported.
16-
from django.apps import AppConfig # noqa
17-
default_app_config = 'ajax_select.apps.AjaxSelectConfig'
18-
except ImportError:
19-
# Previous django versions should load now
20-
# using settings.AJAX_LOOKUP_CHANNELS
21-
registry.load_channels()
11+
# django 1.7+ will use the new AppConfig api
12+
# It will load all your lookups.py modules
13+
# and any specified in settings.AJAX_LOOKUP_CHANNELS
14+
# It will do this after all apps are imported.
15+
from django.apps import AppConfig # noqa
16+
default_app_config = 'ajax_select.apps.AjaxSelectConfig'

ajax_select/apps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class AjaxSelectConfig(AppConfig):
55

66
"""
77
Django 1.7+ enables initializing installed applications
8-
and autodiscovering modules
8+
and autodiscovering modules.
99
1010
On startup, search for and import any modules called `lookups.py` in all installed apps.
1111
Your LookupClass subclass may register itself.

ajax_select/fields.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from django import forms
66
from django.conf import settings
77
from django.contrib.contenttypes.models import ContentType
8-
from django.core.serializers.json import DjangoJSONEncoder
98
from django.db.models import Model
109
from django.forms.utils import flatatt
1110
from django.template.defaultfilters import force_escape
@@ -14,6 +13,7 @@
1413
from django.utils.safestring import mark_safe
1514
from django.utils.six import text_type
1615
from django.utils.translation import ugettext as _
16+
from django.utils.module_loading import import_string
1717

1818
from ajax_select.registry import registry
1919

@@ -40,6 +40,10 @@ def _media(self):
4040
return forms.Media(css={'all': ('ajax_select/css/ajax_select.css',)}, js=js)
4141

4242

43+
json_encoder = import_string(getattr(settings, 'AJAX_SELECT_JSON_ENCODER',
44+
'django.core.serializers.json.DjangoJSONEncoder'))
45+
46+
4347
###############################################################################
4448

4549

@@ -227,7 +231,7 @@ def render(self, name, value, attrs=None):
227231
'current': value,
228232
'current_ids': current_ids,
229233
'current_reprs': mark_safe(
230-
json.dumps(initial, cls=DjangoJSONEncoder)
234+
json.dumps(initial, cls=json_encoder)
231235
),
232236
'help_text': help_text,
233237
'extra_attrs': mark_safe(flatatt(final_attrs)),
@@ -439,7 +443,10 @@ def autoselect_fields_check_can_add(form, model, user):
439443
for name, form_field in form.declared_fields.items():
440444
if isinstance(form_field, (AutoCompleteSelectMultipleField, AutoCompleteSelectField)):
441445
db_field = model._meta.get_field(name)
442-
form_field.check_can_add(user, db_field.rel.to)
446+
if hasattr(db_field, "remote_field"):
447+
form_field.check_can_add(user, db_field.remote_field.model)
448+
else:
449+
form_field.check_can_add(user, db_field.rel.to)
443450

444451

445452
def make_plugin_options(lookup, channel_name, widget_plugin_options, initial):
@@ -457,9 +464,9 @@ def make_plugin_options(lookup, channel_name, widget_plugin_options, initial):
457464
po['html'] = True
458465

459466
return {
460-
'plugin_options': mark_safe(json.dumps(po, cls=DjangoJSONEncoder)),
467+
'plugin_options': mark_safe(json.dumps(po, cls=json_encoder)),
461468
'data_plugin_options': force_escape(
462-
json.dumps(po, cls=DjangoJSONEncoder)
469+
json.dumps(po, cls=json_encoder)
463470
)
464471
}
465472

ajax_select/helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.db.models.fields.related import ForeignKey, ManyToManyField
22
from django.forms.models import ModelForm
3-
from django.utils.text import capfirst
43
from django.utils.encoding import force_text
4+
from django.utils.text import capfirst
55
from django.utils.translation import ugettext_lazy as _
66

77

@@ -84,8 +84,8 @@ def make_ajax_field(related_model, fieldname_on_model, channel, show_help_text=F
8484
(AutoCompleteField, AutoCompleteSelectField, AutoCompleteSelectMultipleField): field
8585
"""
8686
from ajax_select.fields import AutoCompleteField, \
87-
AutoCompleteSelectMultipleField, \
88-
AutoCompleteSelectField
87+
AutoCompleteSelectMultipleField, \
88+
AutoCompleteSelectField
8989

9090
field = related_model._meta.get_field(fieldname_on_model)
9191
if 'label' not in kwargs:

ajax_select/lookup_channel.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ def get_objects(self, ids):
9595
list: list of Model objects
9696
"""
9797
# Inherited models have a OneToOneField (rather than eg AutoField)
98-
if self.model._meta.pk.rel is not None:
98+
if getattr(self.model._meta.pk, "remote_field", False):
99+
# Use the type of the field being referenced (2.0+)
100+
pk_type = self.model._meta.pk.remote_field.field.to_python
101+
elif getattr(self.model._meta.pk, "rel", False):
99102
# Use the type of the field being referenced
100103
pk_type = self.model._meta.pk.rel.field.to_python
101104
else:

ajax_select/models.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

ajax_select/registry.py

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
from django.core.exceptions import ImproperlyConfigured
1+
from django.apps import apps
22
from django.conf import settings
3+
from django.core.exceptions import ImproperlyConfigured
4+
from django.utils.module_loading import autodiscover_modules
35

46

57
class LookupChannelRegistry(object):
@@ -17,13 +19,7 @@ def load_channels(self):
1719
Called when loading the application. Cannot be called a second time,
1820
(eg. for testing) as Django will not re-import and re-register anything.
1921
"""
20-
self._registry = {}
21-
try:
22-
from django.utils.module_loading import autodiscover_modules
23-
except ImportError:
24-
pass
25-
else:
26-
autodiscover_modules('lookups')
22+
autodiscover_modules('lookups')
2723

2824
if hasattr(settings, 'AJAX_LOOKUP_CHANNELS'):
2925
self.register(settings.AJAX_LOOKUP_CHANNELS)
@@ -112,23 +108,7 @@ class MadeLookupChannel(LookupChannel):
112108

113109
def get_model(app_label, model_name):
114110
"""Loads the model given an 'app_label' 'ModelName'"""
115-
try:
116-
# django >= 1.7
117-
from django.apps import apps
118-
except ImportError:
119-
# django < 1.7
120-
from django.db import models
121-
return models.get_model(app_label, model_name)
122-
else:
123-
return apps.get_model(app_label, model_name)
124-
125-
126-
def can_autodiscover():
127-
try:
128-
from django.apps import AppConfig # noqa
129-
except ImportError:
130-
return False
131-
return True
111+
return apps.get_model(app_label, model_name)
132112

133113

134114
def register(channel):

docs/source/Install.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Include the urls in your project::
3636

3737
Write a LookupChannel to specify the models, search queries, formatting etc. and register it with a channel name::
3838

39+
# app_name/lookup.py
3940
from ajax_select import register, LookupChannel
4041
from .models import Tag
4142

@@ -56,6 +57,7 @@ For previous Djangos you can import them manually to your urls or views.
5657
Add ajax lookup fields in your admin.py::
5758

5859
from django.contrib import admin
60+
from ajax_select.admin import AjaxSelectAdmin
5961
from ajax_select import make_ajax_form
6062
from .models import Document
6163

0 commit comments

Comments
 (0)