Ticket #10431: lsaffre_20090307.diff

File lsaffre_20090307.diff, 1.9 KB (added by Luc Saffre, 15 years ago)
  • django/forms/models.py

     
    143143    Returns a ``SortedDict`` containing form fields for the given model.
    144144
    145145    ``fields`` is an optional list of field names. If provided, only the named
    146     fields will be included in the returned fields.
     146    fields will be included in the returned fields. 
    147147
    148148    ``exclude`` is an optional list of field names. If provided, the named
    149149    fields will be excluded from the returned fields, even if they are listed
    150150    in the ``fields`` argument.
     151   
     152    If ``fields`` is provided, the returned list will be in that order,
     153    otherwise as defined in the Model.
    151154    """
    152     # TODO: if fields is provided, it would be nice to return fields in that order
     155    #
    153156    field_list = []
    154157    opts = model._meta
    155     for f in opts.fields + opts.many_to_many:
     158    if fields:
     159        def search(name):
     160            for f in opts.fields + opts.many_to_many:
     161                if name == f.name:
     162                    return f
     163        model_fields = []
     164        for fn in fields:
     165            f = search(fn)
     166            if f:
     167                model_fields.append(f)
     168            # TODO: Otherwise raise an exception here? Which one?
     169    else:
     170        model_fields = opts.fields + opts.many_to_many
     171    for f in model_fields:
    156172        if not f.editable:
    157173            continue
    158         if fields and not f.name in fields:
    159             continue
    160174        if exclude and f.name in exclude:
    161175            continue
    162176        formfield = formfield_callback(f)
     
    164178            field_list.append((f.name, formfield))
    165179    return SortedDict(field_list)
    166180
     181
    167182class ModelFormOptions(object):
    168183    def __init__(self, options=None):
    169184        self.model = getattr(options, 'model', None)
Back to Top