Package: django-yaaac
Yaaacis lightweight Django application providing Ajax search to foreign-key form fields.Version 3 upgrade warningThis version brings Python 3.6 compatibility with a minor regression.
The defaultsuggest_byis now__str__instead of__unicode__so check
theDjango documentationto migrate your code.Django 2-2 and Version 3.2 important noteSince Django-2.2 the wayassets are sorted has been completely rewrittenand as the result breaks this lib in the admin. The fix was to embbed jQuery and set the
dependency in the widgets Media classes which may leads to other regressions.Since aautocomplete solutionis now built-in Django admin, the support for the admin has been removed.InstallationInstall the app from the source:python setup.py build && (sudo) python setup.py installOr withpip:pip install django-yaaac(for Django < 1.8, use a previous version likepip installdjango-yaaac==1.9.0)Add the app in your settings.INSTALLED_APPS:INSTALLED_APPS=[...,"django_yaaac",...,]In theurls.pymodule of your project, define the url pattern for ajax calls:fromdjango_yaaac.managerimportautocompletefromdjango.conf.urlsimporturlurlpatterns=[...url(r'^yaaac/',autocomplete.urls),...]UsageFormsWhat you need to do is to declare a customModelForm:fromdjangoimportformsfromdjango.contribimportadminfromdjango_yaaac.forms.fieldsimportAutocompleteModelChoiceFieldfrom.importmodelsclassBandMemberForm(forms.ModelForm):band=AutocompleteModelChoiceField(site=admin.site,queryset=models.Band.objects.all(),yaaac_opts={"search_fields":["^name"],"suggest_by":"get_full_name","min_chars":3,# Fire search when 3 chars are sent (1 by default.)"max_height":400,# 300px by default."width":250,# 300px by default.},required=True)classMeta:model=models.BandMemberclassMedia:# You need jQuery. Don't forget to call {{ form.media }} in your template.js=('js/jquery.min.js',)admin.site.register(models.BandMember,BandMemberAdmin)Thesiteparameter ofAutocompleteModelChoiceFieldis required for related lookup (the
magnifier glass). Thesearch_fieldsis a list of fields to search against using the same syntax
as in Django Admin (^, $ etc).
Extra optionsmin_chars,max_heightandwidthare the counter-part ofminChars,maxHeightandwidthinAutocomplete options.suggest_byis optional. It can be a field or a method of the model.
By default, suggestions are shown using__unicode__method.If your model define aget_absolute_url()method, the label is a link to that resource.ModelsTheYaaacclass must defines the following:user_passes_testis a class method that takes a user and return True or False.allows_suggest_byis a list of model fields or methods that can used as return value by the search view.classBandMember(models.Model):first_name=models.CharField(max_length=100)last_name=models.CharField(max_length=100)band=models.ForeignKey("Band",null=True,blank=True)favorite_instrument=models.ForeignKey("Instrument",null=True,blank=True)classMeta:unique_together=(('first_name','last_name'),)classYaaac:user_passes_test=lambdainstance,user:useranduser.is_authenticated()orFalseallows_suggest_by=['get_full_name']def__unicode__(self):returnu"%s%s"%(self.first_name,self.last_name)defget_full_name(self):returnu"%s%s"%(self.first_name,self.last_name)TuningTo ease the DOM manipulation, HTML classes are added to the elements. The most interesting beingyaaac_<fieldname>to the hidden input storing the foreign key value. This is very convenient when you need to add behavior to a whole
set of fields - also those that don’t exist when the page is created - sharing the same name.Use jQuery delegation (i.e.$(".foo").on("change",".yaaac_first_name")) to place an event on one field for all
the inline forms present in the page or to come (i.e. Click on “Add a new Band Member”.)