Generic foreign keys are supported since 0.4.
import autocomplete_light
from models import Contact, Address
class MyGenericChannel(autocomplete_light.GenericChannelBase):
def get_querysets(self):
return {
Contact: Contact.objects.all(),
Address: Address.objects.all(),
}
def order_results(self, results):
if results.model == Address:
return results.order_by('street')
elif results.model == Contact:
return results.order_by('name')
def query_filter(self, results):
q = self.request.GET.get('q', None)
if q:
if results.model == Address:
results = results.filter(street__icontains=q)
elif results.model == Contact:
results = results.filter(name__icontains=q)
return results
autocomplete_light.register(MyGenericChannel)
Wraps around multiple querysets, from multiple model classes, rather than just one.
This is also interresting as it overrides all the default model logic from ChannelBase. Hell, you could even copy it and make your CSVChannelBase, a channel that uses a CSV file as backend. But only if you’re really bored or for a milion dollars.
Set result_template and autocomplete_template if necessary.
Return results for each queryset returned by get_querysets().
Note that it limits each queryset’s to self.limit_result. If you want a maximum of 12 suggestions and have a total of 4 querysets, then self.limit_results should be set to 3.
Return results, without doing any ordering.
In most cases, you would not have to override this method as querysets should be ordered by default, based on model.Meta.ordering.
import autocomplete_light
from models import TaggedItem
class TaggedItemForm(autocomplete_light.GenericModelForm):
content_object = autocomplete_light.GenericForeignKeyField(
widget=autocomplete_light.AutocompleteWidget(
'MyGenericChannel', max_items=1))
class Meta:
model = TaggedItem
widgets = autocomplete_light.get_widgets_dict(TaggedItem)
exclude = (
'content_type',
'object_id',
)
This simple subclass of ModelForm fixes a couple of issues with django’s ModelForm.
What ModelForm does, but also add virtual field values to self.initial.
Simple form field that converts strings to models.
Example model with related:
from django.db import models
from django.db.models import signals
from django.contrib.contenttypes import generic
from genericm2m.models import RelatedObjectsDescriptor
class ModelGroup(models.Model):
name = models.CharField(max_length=100)
related = RelatedObjectsDescriptor()
def __unicode__(self):
return self.name
Example generic_m2m.GenericModelForm usage:
import autocomplete_light
from autocomplete_light.contrib.generic_m2m import GenericModelForm, \
GenericManyToMany
from models import ModelGroup
class ModelGroupForm(GenericModelForm):
related = GenericManyToMany(
widget=autocomplete_light.AutocompleteWidget('MyGenericChannel'))
class Meta:
model = ModelGroup
Example ModelAdmin:
from django.contrib import admin
from models import ModelGroup
from forms import ModelGroupForm
class ModelGroupAdmin(admin.ModelAdmin):
form = ModelGroupForm
admin.site.register(ModelGroup, ModelGroupAdmin)
autocomplete_light.contrib.generic_m2m couples django-autocomplete-light with django-generic-m2m.
Generic many to many are supported since 0.5. It depends on django-generic-m2m external apps. Follow django-generic-m2m installation documentation, but at the time of writing it barely consists of adding the genericm2m to INSTALLED_APPS, and adding a field to models that should have a generic m2m relation. So, kudos to the maintainers of django-generic-m2m, fantastic app, use it for generic many to many relations.
See examples in test_project/generic_m2m_example.
Simple form field that converts strings to models.
Extension of autocomplete_light.GenericModelForm, that handles genericm2m’s RelatedObjectsDescriptor.
Add related objects to initial for each generic m2m field.
Yield name, field for each RelatedObjectsDescriptor of the model of this ModelForm.