Django

Code

Ticket #4130: patch_field_choices.txt

File patch_field_choices.txt, 1.3 kB (added by ned@nedbatchelder, 2 years ago)

The patch!

Line 
1 Index: django/db/models/query.py
2 ===================================================================
3 --- django/db/models/query.py   (revision 5061)
4 +++ django/db/models/query.py   (working copy)
5 @@ -853,6 +853,13 @@
6          return None
7      return matches[0]
8  
9 +def field_choices(field_list, related_query):
10 +    if related_query:
11 +        choices = [f.field.related_query_name() for f in field_list]
12 +    else:
13 +        choices = [f.name for f in field_list]
14 +    return choices
15 +
16  def lookup_inner(path, lookup_type, value, opts, table, column):
17      qn = backend.quote_name
18      joins, where, params = SortedDict(), [], []
19 @@ -937,7 +944,11 @@
20      except FieldFound: # Match found, loop has been shortcut.
21          pass
22      else: # No match found.
23 -        raise TypeError, "Cannot resolve keyword '%s' into field" % name
24 +        choices = field_choices(current_opts.many_to_many, False) + \
25 +                    field_choices(current_opts.get_all_related_many_to_many_objects(), True) + \
26 +                    field_choices(current_opts.get_all_related_objects(), True) + \
27 +                    field_choices(current_opts.fields, False)
28 +        raise TypeError, "Cannot resolve keyword '%s' into field, choices are: %s" % (name, ", ".join(choices))
29  
30      # Check whether an intermediate join is required between current_table
31      # and new_table.