Code

Changes between Version 1 and Version 2 of AJAXForeignKey


Ignore:
Timestamp:
05/20/07 13:56:52 (7 years ago)
Author:
Michael Axiak <axiak@…>
Comment:

Michael Axiak

Legend:

Unmodified
Added
Removed
Modified
  • AJAXForeignKey

    v1 v2  
    14141. Create a new folder in your project called {{{ajaxfkey }}} 
    1515 
    16 2. Create three files in that directory: 
     162. Put four files in that directory: 
    1717 
    1818  * {{{__init__.py}}} -- duh 
    1919  * {{{fields.py}}} -- contains the actual model field. (download) 
    2020  * {{{forms.py}}}  -- contains the form field. (download) 
     21  * {{{views.py}}}  -- contains the AJAX view. (download) 
     22 
    21233. Create a {{{base_site.html}}} file in your {{{base_templates/admin}}} directory. 
    2224 
    23 4. Modify your models to use it correctly. 
     254. Modify your {{{urls conf}}} to contain the ajax view. 
    2426 
    25 === {{{ base_templates/admin/base_site.html }}} === 
     275. Modify your models to use it correctly. 
     28 
     29=== Creating {{{ base_templates/admin/base_site.html }}} === 
     30 
     31Put the contents below in the file, but be wary of where yui is on your setup. 
    2632 
    2733{{{ 
     
    4854}}} 
    4955 
    50 Notice how YUI is in {{{"/media/scripts/yui"}}} and I downloaded [http://www.json.org/json.js json.js] 
     56Notice how YUI is in {{{"/media/scripts/yui"}}} and I downloaded [http://www.json.org/json.js json.js]. 
     57 
     58=== Modify {{{urls.py}}} === 
     59 
     60Put the following in your urls configuration: 
     61{{{ 
     62#!python 
     63(r'^admin/ajax_autocomplete/?', 'project.ajaxfkey.views.ajax_autocomplete'), 
     64}}} 
     65 
     66Change {{{project.ajaxfkey}}} to the path to the views file you created. 
     67 
     68=== Using it in your models === 
     69 
     70Below is an example usage in some models: 
     71 
     72{{{ 
     73#!python 
     74from django.db import models 
     75from ajaxfkey.fields import AjaxForeignKey 
     76 
     77# notice the manager, we can create a staticmethod in Poll if we wanted. 
     78class PollManager(models.Manager): 
     79    def ajax_autocomplete(self, data): 
     80        return self.filter(question__istartswith = data).order_by('question') 
     81 
     82class Poll(models.Model): 
     83    question = models.CharField(maxlength=200) 
     84    pub_date = models.DateTimeField('date published') 
     85 
     86class 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}}}