Ticket #1305: django.reverse_lookup.patch

File django.reverse_lookup.patch, 735 bytes (added by andreas@…, 13 years ago)

reverse lookup by related name

  • django/db/models/query.py

     
    239239    Finds a field with a specific name in a list of field instances.
    240240    Returns None if there are no matches, or several matches.
    241241    """
    242     matches = [f for f in field_list if f.name == name]
     242    matches = []
     243    for f in field_list:
     244        if hasattr(f, 'field') and f.field.rel.related_name:
     245            if f.field.rel.related_name == name:
     246                matches.append(f)
     247        elif f.name == name:
     248            matches.append(f)
    243249    if len(matches) != 1:
    244250        return None
    245251    return matches[0]
Back to Top