def __init__(self, user, response_format, instance, *args, **kwargs): super(ObjectLinksForm, self).__init__(*args, **kwargs) queryset = Object.filter_permitted(user, Object.objects) self.fields['links'].queryset = queryset if 'ajax' not in response_format: if instance: queryset = queryset.exclude(pk__in=instance.links.all()) choices = [] for obj in queryset: human_type = obj.get_human_type() name = do_truncate( do_striptags(unicode(obj.object_name)), 20, True) if human_type: name += u" (" + human_type + u")" choices.append((obj.id, name)) self.fields['links'].choices = choices self.fields['links'].label = "" self.fields['links'].initial = "" self.fields['links'].widget.attrs.update({'class': 'autocomplete', 'callback': reverse('core_ajax_object_lookup')})
def __init__(self, user, location_id, *args, **kwargs): super(LocationForm, self).__init__(*args, **kwargs) self.fields['name'].label = _("Name") self.fields['parent'].label = _("Parent") self.fields['parent'].queryset = Object.filter_permitted( user, Location.objects, mode='x') if location_id: self.fields['parent'].initial = location_id
def ajax_object_lookup(request, response_format='html'): "Returns a list of matching objects" objects = [] if request.GET and 'term' in request.GET: objects = Object.filter_permitted(request.user.profile, Object.objects.filter( object_name__icontains=request.GET['term']), mode='x')[:10] return render_to_response('core/ajax_object_lookup', {'objects': objects}, context_instance=RequestContext(request), response_format=response_format)