def get_model_fields(model, fields=None, exclude=None):
    """
    Returns a generator of fields for a model.

    Arguments::

        ``fields`` is an optional list to return only those field names.
        ``exclude`` is an optional list to exclude field names.
    """
    opts = model._meta
    for field in sorted(opts.fields + opts.many_to_many):
        if fields and not field.name in fields:
            continue
        if exclude and field.name in exclude:
            continue
        yield field
