This is an improvement of snippet 253 in that it supports database queries.
Implementing autocompletion for foreign keys takes a few steps:
1) Put the snippet above into <app>/widgets/autocomplete.py.
2) Create a view of your foreign key model (here: Donator) in <app>/donator/views.py:
    from models import Donator
    from widgets.autocomplete import autocomplete_response
    
    def autocomplete(request):
        return autocomplete_response(
            request.REQUEST['text'], Donator, (
                'line_1', 'line_2', 'line_3', 'line_4',
                'line_5', 'line_6', 'line_7', 'line_8',
                '^zip_code', 'location'
            )
        )
This view returns the autocompletion result by searching the fields in the tuple. Each word from the form field must appear at least in one database field.
3) Create a URLconf that points to this new view.
4) In the form where you need the autocompletion, define the widget of the foreign key field as an instance of AutoCompleteField:
    from widget.autocomplete import AutoCompleteField
    
    field.widget = AutoCompleteField(
        url='/donator/autocomplete/'),
        options={'minChars': 3}
    )
The url parameter is the URL connected to the view in step 3), the options dict is passed on to the Ajax.Autocompleter JavaScript object.
Links:
* [Snippet 253](http://www.djangosnippets.org/snippets/253/)
* [Django and scriptaculous integration](http://wiki.script.aculo.us/scriptaculous/show/IntegrationWithDjango)
* [Ajax.Autocompleter](http://wiki.script.aculo.us/scriptaculous/show/Ajax.Autocompleter)
                
                    
                    
                    - ajax
- selection
- database
- autocomplete
- query
- foreign-key
- prototype
- scriptaculous
- protoculous
 
            
            
        
        
        
            
                
                This code provides a Django model for photos based on Flickr, as well as a script to perform a one-way sync between Flickr and a Django installation.
*Please note that the snipped contains code for two files, update.py and a Django model.*
*The two chunks are separated by:*
    
    """
    END OF FLICKRUPDATE
    """
    """
    START DJANGO PHOTO MODEL
    Requires django-tagging (http://code.google.com/p/django-tagging/)
    """
My model implements tagging in the form of the wonderful django-tagging app by Jonathan Buchanan, so be sure to install it before trying to use my model.
The flickrupdate.py code uses a modified version of flickerlib.py (http://code.google.com/p/flickrlib/).  Flickr returns invalid XML occasionally, which Python won't stand for.  I got around this by wrapping the return XML in `<flickr_root>` tags.
To modify flickrlib to work with my code, simply change the this line:
    
    return self.parseData(getattr(self._serverProxy, '.'.join(n))(kwargs))
    
to:
    
    return self.parseData('<flickr_root>' + getattr(self._serverProxy, '.'.join(n))(kwargs) + '</flickr_root>')
    
I hate this workaround, but I can't control what Flickr returns.
flickrupdate will hadle the addition and deletion of photos, sets and tags.  It will also keep track of photos' pools, although, right now, it doesn't delete unused pools.  This is mostly because I don't care about unused pools hanging around.  It's a simple enough addition, so I'll probably add it when I have a need.
Be sure to set the appropriate information on these lines:
    
    api_key = "YOUR API KEY"
    api_secret = "YOUR FLICKR SECRET"
    flickr_uid = 'YOUR FLICKR USER ID'
I hadn't seen a Django model and syncing script, so I threw these together.  I hope they will be useful to those wanting start syncing their photos.
                
                    
                    
                    - flickr
- photos
- photo
- flicker
 
            
            
        
        
        
            
                
                Makes a call to Google's geocoder and returns the latitude and longitude as a string or returns an empty string. Called by the save method to save the lat and long to the db to be used when rendering maps on the frontend. Reduces the number of calls to geocoder by calling only when saving, not on every viewing of the object.
Be sure to import *urllib* and the project's *settings*, and to define GOOGLE_API_KEY in settings.py.
**Example:**
    def save(self):
        location = "%s+%s+%s+%s" % (self.address, self.city, self.state, self.zip_code)
        self.lat_long = get_lat_long(location)
        if not self.lat_long:
            location = "%s+%s+%s" % (self.city, self.state, self.zip_code)
            self.lat_long = get_lat_long(location)
        super(Foo, self).save()
                
                    
                    
                    - google-maps
- geocode
- google-api
- latitude
- longitude