| 1 | = An AJAX !ComboBox Widget for Django = |
| 2 | |
| 3 | This widget was written to replace the standard {{{<select>}}} form element Django uses for releated objects in forms. The widget is an extended version of the [http://dojotoolkit.org Dojo] !ComboBox widget. I use this mostly in places where the number of objects in the related objects table exceeds 20 or more. The widget is extracted from a project where some tables have more than 30,000 objects and using a {{{<select>}}} dropdown simply did not scale. |
| 4 | |
| 5 | This widget supports live lookup of related fields based on a single field in that model and autocompletion. |
| 6 | |
| 7 | Credit goes to Eric Moritz's original implementation for inspiring this one. |
| 8 | |
| 9 | == Requirements == |
| 10 | |
| 11 | * [http://cheeseshop.python.org/pypi/simplejson simplejson 1.3] - used for object serialization. |
| 12 | |
| 13 | * [http://dojotoolkit.org Dojo] - used for AJAX and widget implementation. I have been using the latest SVN trunk in my project but version 0.2.2 may work for you. |
| 14 | |
| 15 | * The widget tarball attached to this page. |
| 16 | |
| 17 | |
| 18 | == Installation == |
| 19 | |
| 20 | * Install simplejson and make sure it is somewhere in your PYTHONPATH. |
| 21 | |
| 22 | * Install Dojo in media/js. |
| 23 | |
| 24 | * Untar nongcombobox-0.9.tar.gz and copy the {{{nong}}} directory into the same directory as dojo and copy the contents of {{{views.py}}} into one of your own {{{views.py}}} files. |
| 25 | |
| 26 | My project layout is as follows: |
| 27 | |
| 28 | {{{ |
| 29 | myproject/ |
| 30 | myapp/ |
| 31 | models.py |
| 32 | views.py |
| 33 | urls.py |
| 34 | templates/ |
| 35 | myapp/ |
| 36 | mymodel_form.html |
| 37 | media/ |
| 38 | js/ |
| 39 | dojo/ |
| 40 | nong/ |
| 41 | }}} |
| 42 | |
| 43 | |
| 44 | == Example Use == |
| 45 | |
| 46 | In this example we will create a form that uses the !ComboBox widget to for the Article's reporter field in below. |
| 47 | |
| 48 | === Model === |
| 49 | Using the example model from [http://www.djangoproject.com/documentation/models/many_to_one/ Model Examples]: |
| 50 | |
| 51 | {{{ |
| 52 | #!python |
| 53 | from django.db import models |
| 54 | |
| 55 | class Reporter(models.Model): |
| 56 | first_name = models.CharField(maxlength=30) |
| 57 | last_name = models.CharField(maxlength=30) |
| 58 | email = models.EmailField() |
| 59 | |
| 60 | def __str__(self): |
| 61 | return "%s %s" % (self.first_name, self.last_name) |
| 62 | |
| 63 | class Article(models.Model): |
| 64 | headline = models.CharField(maxlength=100) |
| 65 | pub_date = models.DateField() |
| 66 | reporter = models.ForeignKey(Reporter) |
| 67 | |
| 68 | def __str__(self): |
| 69 | return self.headline |
| 70 | }}} |
| 71 | |
| 72 | === URLconf === |
| 73 | |
| 74 | Add a url to the applications {{{urls.py}}} for the reporter_lookup view like so: |
| 75 | |
| 76 | {{{ |
| 77 | #!python |
| 78 | from myproject.myapp.models import Reporter |
| 79 | |
| 80 | reporter_lookup = { |
| 81 | 'queryset': Reporter.objects.all(), |
| 82 | 'field': 'first_name', # this is the field which is searched |
| 83 | #'limit': 10, # default is to limit query to 10 results. Increase this if you like. |
| 84 | #'login_required': False, # default is to allow anonymous queries. Set to True if you want authenticated access. |
| 85 | } |
| 86 | |
| 87 | urlpatterns = pattern('', |
| 88 | (r'^reporter_lookup/$', 'myproject.myapp.views.json_lookup', reporter_lookup), |
| 89 | ) |
| 90 | }}} |
| 91 | |
| 92 | Now test your new reporter_lookup view my going to {{{http://localhost:8000/reporter_lookup/?search=}}}, which should return something like: |
| 93 | {{{ |
| 94 | [["Reporter 1", 1], ["Reporter 2", 2], ...] |
| 95 | }}} |
| 96 | |
| 97 | === Template === |
| 98 | |
| 99 | Normally you would have just put {{{{{ form.reporter }}}}} somewhere in your {{{article_form.html}}} template to get a drop down menu with a list of the Reporters. To use the new widget in your form you'll need to add the following to your template: |
| 100 | |
| 101 | {{{ |
| 102 | ... |
| 103 | {% block extrahead %} |
| 104 | {% comment %} |
| 105 | load dojo and the combobox widget |
| 106 | {% endcomment %} |
| 107 | <script type="text/javascript" src="/media/js/dojo/dojo.js"></script> |
| 108 | <script type="text/javascript"> |
| 109 | dojo.require("dojo.widget.*"); |
| 110 | dojo.setModulePrefix("nong.widget","../nong/widget"); |
| 111 | dojo.widget.manager.registerWidgetPackage("nong.widget"); |
| 112 | dojo.require("nong.widget.NongComboBox"); |
| 113 | </script> |
| 114 | |
| 115 | {% comment %} |
| 116 | Add the following if you use the same template for both adding and changing. This block pre-populates the new combobox widget with the current value of the article's reporter. |
| 117 | {% endcomment %} |
| 118 | {% if object %} |
| 119 | <script type="text/javascript"> |
| 120 | function fillComboBoxes() { |
| 121 | var reporter = dojo.widget.byId("{{ form.reporter.get_id }}"); |
| 122 | reporter.setValue("{{ object.reporter.name }}"); |
| 123 | reporter.setSelectedValue("{{ object.reporter.id }}"); |
| 124 | }; |
| 125 | dojo.addOnLoad(fillComboBoxes); |
| 126 | </script> |
| 127 | {% endif %} |
| 128 | |
| 129 | {% endblock %} |
| 130 | ... |
| 131 | {% comment %} |
| 132 | Replace the usual {{ form.reporter }} element in the template with this: |
| 133 | {% endcomment %} |
| 134 | <select dojoType="NongComboBox" id="{{ form.reporter.get_id }}" name="reporter" dataUrl="/reporter_lookup/?search=%{searchString}" mode="remote" value="default"></select> |
| 135 | ... |
| 136 | }}} |
| 137 | |
| 138 | That's it! You should be able to submit this form just like you normally would. |