Request Method: GET
Request URL: http://127.0.0.1:8000/people/person/
Exception Type: TypeError
Exception Value: Cannot resolve keyword '' into field. Choices are:
organizationdatabase, meterreadings, transaction, id, pid, location,
street, building, corpus, block, appartment, first_name, last_name,
middle_name, hd, population, space, rooms, privilege, comment
Exception Location: /home/sector119/devel/django_src/django/db/models/
query.py in lookup_inner, line 1044
Python Executable: /usr/bin/python2.5
Python Version: 2.5.1
I got this exception when I use Q objects. When I use queryset =
queryset.filter(last_name=query) everything is ok, why?
def object_search(request, queryset, paginate_by=None, page=None,
allow_empty=True, template_name=None, template_loader=loader,
extra_context=None, context_processors=None,
template_object_name='object', mimetype=None):
import operator
from django.http import HttpResponseRedirect
from django.db.models import Q
from views.generic import forms
SEARCH_VAR = 'q'
def construct_search(field_name):
if field_name.startswith('^'):
return "%s__istartswith" % field_name[1:]
elif field_name.startswith('='):
return "%s__iexact" % field_name[1:]
elif field_name.startswith('@'):
return "%s__search" % field_name[1:]
else:
return "%s__icontains" % field_name
queryset = queryset._clone()
query = request.GET.get(SEARCH_VAR, '')
if query:
search_fields = queryset.model._meta.admin.search_fields
if search_fields:
clause = []
for bit in query.split():
clause.extend([Q(**{construct_search(field_name):
bit}) for field_name in search_fields])
clause = reduce(operator.or_, clause)
queryset = queryset.filter(clause)
form = forms.ObjectSearchForm(initial={SEARCH_VAR:
query})
return object_list(request=request, queryset=queryset,
paginate_by=paginate_by, extra_context={SEARCH_VAR: query, 'form':
form})
class Person(models.Model):
id = fields.PositiveBigIntegerField(_('Global person id'),
help_text=_('Global person id: location id concatenated with person
id.'), primary_key=True)
pid = models.IntegerField(_('Person id'))
location = models.ForeignKey(Location, verbose_name=_('The related
location.'))
street = models.ForeignKey(Street, verbose_name=_('The related
street.'))
building = models.PositiveSmallIntegerField(_('Building'))
corpus = models.CharField(_('Corpus'), max_length=1, blank=True)
block = models.CharField(_('Block'), max_length=1, blank=True)
appartment = models.PositiveSmallIntegerField(_('Appartment'),
blank=True)
first_name = models.CharField(_('First name'), max_length=50)
last_name = models.CharField(_('Last name'), max_length=50,
blank=True)
middle_name = models.CharField(_('Middle name'), max_length=50,
blank=True)
def _get_full_name(self):
if len(self.first_name) > 0 and len(self.middle_name) > 0:
return u'%s %s. %s.' % (self.last_name,
self.first_name[0], self.middle_name[0])
else:
return self.last_name
def __unicode__(self):
return self._get_full_name()
class Meta:
unique_together = ('location', 'pid')
ordering = ['last_name', 'first_name', 'middle_name']
class Admin:
search_fields = ('^last_name')
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---