Django

Code

Ticket #4130: field_choices.patch

File field_choices.patch, 4.6 kB (added by Collin Grady <cgrady@the-magi.us>, 2 years ago)

updated patch to trunk, fixed paths in test patch, combined both

  • django/db/models/query.py

    old new  
    853853        return None 
    854854    return matches[0] 
    855855 
     856def field_choices(field_list, related_query): 
     857    if related_query: 
     858        choices = [f.field.related_query_name() for f in field_list] 
     859    else: 
     860        choices = [f.name for f in field_list] 
     861    return choices 
     862 
    856863def lookup_inner(path, lookup_type, value, opts, table, column): 
    857864    qn = backend.quote_name 
    858865    joins, where, params = SortedDict(), [], [] 
     
    937944    except FieldFound: # Match found, loop has been shortcut. 
    938945        pass 
    939946    else: # No match found. 
    940         raise TypeError, "Cannot resolve keyword '%s' into field" % name 
     947        choices = field_choices(current_opts.many_to_many, False) + \ 
     948            field_choices(current_opts.get_all_related_many_to_many_objects(), True) + \ 
     949            field_choices(current_opts.get_all_related_objects(), True) + \ 
     950            field_choices(current_opts.fields, False) 
     951        raise TypeError, "Cannot resolve keyword '%s' into field, choices are: %s" % (name, ", ".join(choices)) 
    941952 
    942953    # Check whether an intermediate join is required between current_table 
    943954    # and new_table. 
  • tests/modeltests/reverse_lookup/models.py

    old new  
    5555>>> Poll.objects.get(choice__name__exact="This is the answer") 
    5656Traceback (most recent call last): 
    5757    ... 
    58 TypeError: Cannot resolve keyword 'choice' into field 
     58TypeError: Cannot resolve keyword 'choice' into field, choices are: poll_choice, related_choice, id, question, creator 
    5959"""} 
  • tests/modeltests/lookup/models.py

    old new  
    223223>>> Article.objects.filter(pub_date_year='2005').count() 
    224224Traceback (most recent call last): 
    225225    ... 
    226 TypeError: Cannot resolve keyword 'pub_date_year' into field 
     226TypeError: Cannot resolve keyword 'pub_date_year' into field, choices are: id, headline, pub_date 
    227227 
    228228>>> Article.objects.filter(headline__starts='Article') 
    229229Traceback (most recent call last): 
    230230    ... 
    231 TypeError: Cannot resolve keyword 'headline__starts' into field 
     231TypeError: Cannot resolve keyword 'headline__starts' into field, choices are: id, headline, pub_date 
    232232 
    233233"""} 
  • tests/modeltests/many_to_one/models.py

    old new  
    174174>>> Article.objects.filter(reporter_id__exact=1) 
    175175Traceback (most recent call last): 
    176176    ... 
    177 TypeError: Cannot resolve keyword 'reporter_id' into field 
     177TypeError: Cannot resolve keyword 'reporter_id' into field, choices are: id, headline, pub_date, reporter 
    178178 
    179179# You need to specify a comparison clause 
    180180>>> Article.objects.filter(reporter_id=1) 
    181181Traceback (most recent call last): 
    182182    ... 
    183 TypeError: Cannot resolve keyword 'reporter_id' into field 
     183TypeError: Cannot resolve keyword 'reporter_id' into field, choices are: id, headline, pub_date, reporter 
    184184 
    185185# You can also instantiate an Article by passing 
    186186# the Reporter's ID instead of a Reporter object. 
  • tests/modeltests/custom_columns/models.py

    old new  
    7171>>> Author.objects.filter(firstname__exact='John') 
    7272Traceback (most recent call last): 
    7373    ... 
    74 TypeError: Cannot resolve keyword 'firstname' into field 
     74TypeError: Cannot resolve keyword 'firstname' into field, choices are: article, id, first_name, last_name 
    7575 
    7676>>> a = Author.objects.get(last_name__exact='Smith') 
    7777>>> a.first_name 
  • tests/regressiontests/null_queries/models.py

    old new  
    3232>>> Choice.objects.filter(foo__exact=None)  
    3333Traceback (most recent call last): 
    3434... 
    35 TypeError: Cannot resolve keyword 'foo' into field 
     35TypeError: Cannot resolve keyword 'foo' into field, choices are: id, poll, choice 
    3636 
    3737# Can't use None on anything other than __exact 
    3838>>> Choice.objects.filter(id__gt=None)