Autocomplete on Inlines ForeignKey

125 views
Skip to first unread message

Vinicius Massuchetto

unread,
May 24, 2011, 6:51:10 AM5/24/11
to Django Users
Have anyone been able to implement an autocomplete input field in an
inline formset? If so, please provide some references.

The main apps that do this in a normal form can't render the same
behavior in an inline.

Many thanks.
--
Vinicius Massuchetto
http://vinicius.soylocoporti.org.br

Karen Tracey

unread,
May 24, 2011, 8:10:35 AM5/24/11
to django...@googlegroups.com
On Tue, May 24, 2011 at 6:51 AM, Vinicius Massuchetto <viniciusm...@gmail.com> wrote:
Have anyone been able to implement an autocomplete input field in an
inline formset? If so, please provide some references.

The main apps that do this in a normal form can't render the same
behavior in an inline.


There's a ticket open (https://code.djangoproject.com/ticket/15760) for adding some JS hooks for dynamic inlines in admin. However your post doesn't mention the word dynamic or admin so I'm not sure it's what you are looking for...

Karen
--
http://tracey.org/kmt/

Vinicius Massuchetto

unread,
May 24, 2011, 8:47:06 AM5/24/11
to django...@googlegroups.com
2011/5/24 Karen Tracey <kmtr...@gmail.com>:

I don't think it's exactly what I'm looking for. I need to implement
ajax completion in some fields in an inline form, it would be
something like `django-autocomplete` and `django-ajax-select` can do
in usual forms.

Any help is appreciated.
Thanks.

Sells, Fred

unread,
May 24, 2011, 11:36:56 AM5/24/11
to django...@googlegroups.com
I'm using AJAX, jquery and JSON for the first time, having used
templates to render XML for Flex/Flash in all my previous Django work.
My code looks like this

records = models.Residents.objects.extra( where=[....], params=[...])
data = serializers.serialize('json', records, ensure_ascii=False,
fields=('fname','lname', 'pt'))
return HttpResponse(data)

After experimenting the "ensure_ascii=False" seems to get rid of the
Unicode prefix which is not used in my work.

which returns
[
{"pk": "77777", "model": "app.residents", "fields": {"lname": "Mouse ",
"pt": "0", "fname": "Minnie "}},
...]

I was surprised to see the subnode "fields" in the output. Perhaps I'm
just old school having does basic cgi with json and pretty much forced
the format.

1. However is the above the "best practice" or is there an option to
strip the meta data.

2. Does someone have a "best practice" jquery snippet to work with these
nested fields.

3. Would it be a cleaner solution to use a template to return some
"inner html" that ajax would use without any parsing logic.

I apologize for asking such basic questions, but I'm working solo and
this is my first serious HTML5 project after mostly using Flex and XML.

Thanks.

Ian Clelland

unread,
May 27, 2011, 3:00:51 AM5/27/11
to django...@googlegroups.com
On Tue, May 24, 2011 at 8:36 AM, Sells, Fred <fred....@adventistcare.org> wrote:
My code looks like this

records = models.Residents.objects.extra( where=[....], params=[...])
data = serializers.serialize('json', records, ensure_ascii=False,
fields=('fname','lname', 'pt'))
return HttpResponse(data)

After experimenting the "ensure_ascii=False" seems to get rid of the
Unicode prefix which is not used in my work.

which returns
[
{"pk": "77777", "model": "app.residents", "fields": {"lname": "Mouse ",
"pt": "0", "fname": "Minnie "}},
...]

I was surprised to see the subnode "fields" in the output.  Perhaps I'm
just old school having does basic cgi with json and pretty much forced
the format.

1. However is the above the "best practice" or is there an option to
strip the meta data.

I don't think that anyone would condone this as a 'best practice' -- the Django serializers are meant to dump django objects, say into session data, or into database fixtures, and they are really designed to be read by the deserializers when the object needs to be reconstructed.

If you are passing data from Django to a browser's JavaScript thread using JSON, then you probably want to either

1. Use an API framework, such as Piston (google: django-piston) or TastyPie (google: django-tastypie) to handle serialization of outgoing data. These are very useful if you expect to be receiving data from the browser in the same format for creating or updating objects, but they can be a lot of overhead for small applications, so you may want to

2. Write your own serialization. It's quite simple, and Django does an excellent job of packaging the simplejson library (deferring to the system installed version, if it's available, and newer than Django's). All you need to do is something like this:

    from django.utils import simplejson as json
    ...
    records = models.Residents.objects.extra( where=[....], params=[...])
    data = json.dumps(records.values('fname','lname','pt'))
    return HttpResponse(data, mimetype='application/json')

records.values(...) should return just the dictionary that you want to use. json.dumps(...) will convert that into a JSON string, which you can then return as an HttpResponse.


-- 
Regards,
Ian Clelland
<clel...@gmail.com>
Reply all
Reply to author
Forward
0 new messages