| 50 | | Notice how YUI is in {{{"/media/scripts/yui"}}} and I downloaded [http://www.json.org/json.js json.js] |
| | 56 | Notice how YUI is in {{{"/media/scripts/yui"}}} and I downloaded [http://www.json.org/json.js json.js]. |
| | 57 | |
| | 58 | === Modify {{{urls.py}}} === |
| | 59 | |
| | 60 | Put the following in your urls configuration: |
| | 61 | {{{ |
| | 62 | #!python |
| | 63 | (r'^admin/ajax_autocomplete/?', 'project.ajaxfkey.views.ajax_autocomplete'), |
| | 64 | }}} |
| | 65 | |
| | 66 | Change {{{project.ajaxfkey}}} to the path to the views file you created. |
| | 67 | |
| | 68 | === Using it in your models === |
| | 69 | |
| | 70 | Below is an example usage in some models: |
| | 71 | |
| | 72 | {{{ |
| | 73 | #!python |
| | 74 | from django.db import models |
| | 75 | from ajaxfkey.fields import AjaxForeignKey |
| | 76 | |
| | 77 | # notice the manager, we can create a staticmethod in Poll if we wanted. |
| | 78 | class PollManager(models.Manager): |
| | 79 | def ajax_autocomplete(self, data): |
| | 80 | return self.filter(question__istartswith = data).order_by('question') |
| | 81 | |
| | 82 | class Poll(models.Model): |
| | 83 | question = models.CharField(maxlength=200) |
| | 84 | pub_date = models.DateTimeField('date published') |
| | 85 | |
| | 86 | class Choice(models.Model): |
| | 87 | # notice it's the same syntax as models.ForeignKey |
| | 88 | poll = AjaxForeignKey(Poll) |
| | 89 | choice = models.CharField(maxlength=200) |
| | 90 | votes = models.IntegerField() |
| | 91 | }}} |