Ticket #5634: django.newforms.models.py.diff

File django.newforms.models.py.diff, 1.4 KB (added by MarioGonzalez <gonzalemario @…>, 17 years ago)
  • django/newforms/models.py

     
    6666        return save_instance(self, instance, fields, fail_message, commit)
    6767    return save
    6868
    69 def form_for_model(model, form=BaseForm, fields=None, formfield_callback=lambda f: f.formfield()):
     69def form_for_model(model, form=BaseForm, fields=None, queryset=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
    7070    """
    7171    Returns a Form class for the given Django model class.
    7272
     
    7676    determining the formfield for a given database field. It's a callable that
    7777    takes a database Field instance and returns a form Field instance.
    7878    """
     79    from django.db.models.fields.related import ManyToManyField
    7980    opts = model._meta
    8081    field_list = []
    8182    for f in opts.fields + opts.many_to_many:
     
    8384            continue
    8485        if fields and not f.name in fields:
    8586            continue
    86         formfield = formfield_callback(f)
     87        if isinstance(f, ManyToManyField) and queryset is not None:
     88            formfield = formfield_callback(f, queryset=queryset)
     89        else:
     90            formfield = formfield_callback(f)
    8791        if formfield:
    8892            field_list.append((f.name, formfield))
    8993    base_fields = SortedDictFromList(field_list)
Back to Top