views:

582

answers:

3

Hi at all, I am looking for a working simple example on autocomplete widget for foreign key in Django. I have found many example but I don't understand and doesn't work.

This is my model:

class profile(models.Model):

user = models.ForeignKey(User, unique=True)

class profileForm(ModelForm):

user = forms.CharField(widget=AutoCompleteWidget())

class Meta:
    model = profile

I wish an AutoCompleteWidget working for this example.

Could anyone help me pleaseee ?

+2  A: 

Do you want this for the admin interface or the front end?

I've done this in the past in the front-end and it's super easy. I'm about to try it in the admin myself.

Here is a blog post that details how to do this in the admin interface. (With a different model, but you can just translate to your specific example.)

You could adapt that to the front end, or you could follow this super-easy tutorial that comes with all of the JS bundled into a file. That's how I did it the first time. The original blog post seems to be locked, so here's a link to Google's cached version.

Ellie P.
thank you very useful
xRobot
Sure, glad I can help. If this answers your question, consider accepting it as an answer to help people who come to this thread later. I notice there's another question you've asked with an answer that needs accepting. Here's more information about how it works: http://meta.stackoverflow.com/questions/5234/accepting-answers-what-is-it-all-about
Ellie P.
A: 

you might find some useful info here: http://www.michelepasin.org/techblog/?tag=autocomplete

magicrebirth
A: 

I wrote about a solution for the admin, for foreign keys: http://albertoconnor.ca/blog/2010/Sep/3/autocomplete-foreign-keys-in-django. Hopefully this helps.

amjoconn