16

can anybody post the simplest possible example for django-ajax-selects ? Just one model with a field and a form that can be used to select instances of this model based on that field (not in admin but in a user form).

I tried reading the documentation of the project but found it very difficult to understand... Also, I was not able to make the Example App work (coudln't find out what is a Label ???) :(

Also, if you believe that there is an easiest solution than django-ajax-selects please tell me.

Thank you!

Serafeim
  • 14,962
  • 14
  • 91
  • 133
  • 1
    "coudln't find out what is a Label ???" +1 I agree that the documentation of ajax selects is unclear to ajax newbies. I am also wondering what Label is... – Private Nov 04 '13 at 07:25

2 Answers2

7

Here is a simple example (from the example on github but not tested):

models.py:

class Person(models.Model):
    name = models.CharField(blank=True, max_length=100)
    email = models.EmailField()

    def __unicode__(self):
        return self.name


class Group(models.Model):
    name = models.CharField(max_length=200,unique=True)
    members = models.ManyToManyField(Person,blank=True,help_text="Enter text to search for and add each member of the group.")

    def __unicode__(self):
        return self.name

forms.py:

class GroupForm(ModelForm):

    class Meta:
        model = Group

    members  = make_ajax_field(Release,'members','person')

lookups.py:

class PersonLookup(LookupChannel):

    model = Person

    def get_query(self,q,request):
        return Person.objects.filter(name__icontains=q).order_by('name')

    def get_result(self,obj):
        return obj.name

    def format_match(self,obj):
        return self.format_item_display(obj)

    def format_item_display(self,obj):
        return u"%s" % escape(obj.name)

settings.py:

AJAX_LOOKUP_CHANNELS = {
     'person' : ('example.lookups', 'PersonLookup'),
}

views.py:

class Create(generic.CreateView):
    template_name = "create.html"
    form_class = GroupForm
    success_url = 'create'

create = Create.as_view()

urls.py:

urlpatterns = patterns('',
    url(r'^create',  view='views.create',name='create'),
    url(r'^ajax_lookup/(?P<channel>[-\w]+)$', 'ajax_select.views.ajax_lookup', name = 'ajax_lookup'),
)
user1257144
  • 210
  • 4
  • 7
  • Could you upload this as a complete django project on github or bitbucket ? I would like to clone the project and just run "python manage.py runserver" to see how it in action ... Thanks ! – Serafeim Jun 22 '12 at 14:23
0

You should go through the Quick installation guide to get started.

A Label in this documentation is an example model, just like it could be Car, Widget, whatever model you have and want.

jpic
  • 32,891
  • 5
  • 112
  • 113
  • 2
    I tried but couldn't understand :( Why is the label used ? I see that in the AJAX_LOOKUP_CHANNELS a 'person' is used. Why there are both person and label models - I'd like to only use autocomplete for persons not for labels ! How could I put the autocomplete in one of my forms ? And also, why should I register anything under the admin if I don't want to use the autocomplete functionality in the admin ??? – Serafeim Apr 19 '12 at 11:50
  • Why is Label used ? As an example. Obviously, you don't have to register anything in the admin if you don't want autocompletes in the admin ... – jpic Apr 19 '12 at 13:39
  • I just can't be on the mind of the documentation author :( ! If you have a simple working complete example please post it, thanks. – Serafeim Apr 19 '12 at 19:18
  • I'm struggling with this too--there's quite a lot in the documentation about the admin, but it doesn't seem to go with the example. I'm not sure how to use the information about the admin, or if I should at all. – thumbtackthief Nov 02 '13 at 21:48
  • Maybe try django-autocomplete-light ? (disclamer: i make this app) – jpic Nov 03 '13 at 13:37
  • God damn it, I fought for many days with django-ajax-selects with no success, and I was done in thirty minutes with autocomplete-light, the time to copy the examples from the documentation. Bravo et merci to its creator ! – Ehvince Aug 15 '14 at 20:51
  • "Light" works like a charm, but it was difficult to find out that 2 jquery.js are needed: one before the js-autocomplete files(textwidget, remote,etc.) required by autoc. and one after them (required by the admin). – Timo Oct 04 '14 at 19:30
  • Yep, it's not compatible with the old jquery version that django.contrib.admin depends on. However, did you miss the last step of the tutorial ? http://django-autocomplete-light.readthedocs.org/en/v2/install.html#optionally-include-it-in-admin-base-site-html-too – jpic Oct 05 '14 at 16:47