Opened 12 years ago

Closed 7 years ago

Last modified 6 years ago

#2331 closed defect (invalid)

Related Field has invalid lookup: icontains

Reported by: ian@… Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version:
Severity: normal Keywords:
Cc: gmludo@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Malcolm Tredinnick)

just upgraded to the latest head, and found this.
I could have sworn it was working beforehand.

Traceback (most recent call last):
File "/home/data/py/django/core/handlers/" in get_response
  74. response = callback(request, *callback_args, **callback_kwargs)
File "/home/data/py/zilbo/common/utils/views/" in _wrapper
  37. return generic(request, queryset.filter(**filter), *args, **kw)
File "/home/data/py/django/views/generic/" in object_list
  45. object_list = paginator.get_page(page - 1)
File "/home/data/py/django/core/" in get_page
  36. object_list = list(self.query_set[offset:offset+limit])
File "/home/data/py/django/db/models/" in __iter__
  103. return iter(self._get_data())
File "/home/data/py/django/db/models/" in _get_data
  430. self._result_cache = list(self.iterator())
File "/home/data/py/django/db/models/" in iterator
  171. select, sql, params = self._get_sql_clause()
File "/home/data/py/django/db/models/" in _get_sql_clause
  444. joins2, where2, params2 = self._filters.get_sql(opts)
File "/home/data/py/django/db/models/" in get_sql
  574. joins2, where2, params2 = val.get_sql(opts)
File "/home/data/py/django/db/models/" in get_sql
  622. return parse_lookup(self.kwargs.items(), opts)
File "/home/data/py/django/db/models/" in parse_lookup
  734. joins2, where2, params2 = lookup_inner(path, lookup_type, value, opts, opts.db_table, None)
File "/home/data/py/django/db/models/" in lookup_inner
  906. params.extend(field.get_db_prep_lookup(lookup_type, value))
File "/home/data/py/django/db/models/fields/" in get_db_prep_lookup
  105. raise TypeError, "Related Field has invalid lookup: %s" % lookup_type

  TypeError at /asset/machine/
  Related Field has invalid lookup: icontains

Change History (12)

comment:1 Changed 12 years ago by Malcolm Tredinnick

Description: modified (diff)

Could you provide an example of what you were doing to trigger this error, please. It is impossible to work it out just from the traceback. A model and a query against it that fails reliably would be nice. Thanks.

comment:2 Changed 12 years ago by Malcolm Tredinnick

OK, ignore my last comment, it was lazy. I looked at the code. Suspicion falls on [3246], but it looks like Russell was not expecting non-standard primary keys in there (maybe?). Are you seeing this on a model that has a custom primary key? If not, why on earth are we in that method?

I'm reluctant to go diving in here trying to fix edge cases whilst Adrian is doing his refactoring. The odds of a patch surviving are small. The whole query dissecting and processing phase is possibly open on his lab table with entrails all over the place.

comment:3 Changed 12 years ago by Ian@…

It was actually an error on my part.. the field it was it is looking at is a generic relation  similar to one in 
class Forum:: summary_tags

but the error looked weird and didn't use to fail in such a way a week or two ago.

comment:4 Changed 12 years ago by Malcolm Tredinnick

I'm confused, then. Is this a bug in Django or just a confusing error?

comment:5 Changed 11 years ago by Chris Beaven

Resolution: invalid
Status: newclosed

Closing as Ian stated it was an error on his part. Ian: If there's still a problem with Django that needs to be fixed, please reopen.

comment:6 Changed 9 years ago by AntonBessonov

This error raised if use search_fields in E.x:

class AnotherModel(models.Model):
  txt = models.CharField(_('Text'), max_length=255)

class MyModel(models.Model):
  prop = models.ForeignKey(AnotherModel)

You must use:

class MyModelAdmin(admin.ModelAdmin):
  search_fields = ('prop__txt')

Maybe must raise properly exception?

comment:7 Changed 7 years ago by Jeff Blaine

Easy pickings: unset
Resolution: invalid
Status: closedreopened

I'm getting this in 1.3, some 2 years later than the last comment above.

Same situation as above, using Admin. Any search results in the same error as this ticket:

class DeviceAdmin(admin.ModelAdmin):
    search_fields = ['name', 'hardware_arch', 'afs_sysname', 'afs_client_rev', '
propertyno', 'calc_roles', 'notes']

This doesn't work either, where 'hardware_arch' is the only foreign key referenced in search_fields:

class HardwareArchitecture(models.Model):
    name            = models.CharField('Architecture',

class Device(models.Model):
    hardware_arch   = models.ForeignKey(HardwareArchitecture,

class DeviceAdmin(admin.ModelAdmin):
    search_fields = ['name', 'hardware_arch__name', 'afs_sysname', 'afs_client_r
ev', 'propertyno', 'calc_roles', 'notes']

Last edited 7 years ago by Jeff Blaine (previous) (diff)

comment:8 Changed 7 years ago by Jeff Blaine

Resolution: invalid
Status: reopenedclosed

Ignore me. I am an idiot. Fixed. Re-resolving as invalid.

comment:9 Changed 7 years ago by gmludo@…

Cc: gmludo@… added

If like me, you're looking for the solution of this error message, this is the documentation :

comment:10 Changed 6 years ago by i-am@…

UI/UX: unset


haha, thanks!

comment:11 Changed 6 years ago by anonymous

@gmludo me too, thanks for pointing it out... Uwe

comment:12 Changed 6 years ago by anonymous

Thank you.. This problem had really given me some headache

Note: See TracTickets for help on using tickets.
Back to Top