models.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:djangorestframework-utils 作者: benzid-wael 项目源码 文件源码
def modelserializer_factory(model, serializer=None, fields=None, exclude=None):
    """
    Returns a ModelSerializer containing fields for the given model.

    :param model: model class.
    :param fields: is an optional list of field names. If provided, only the named
    fields will be included in the returned fields. If omitted or '__all__', all
    fields will be used.
    :param exclude: is an optional list of field names. If provided, the named fields
    will be excluded from the returned fields, even if they are listed in the ``fields``
    argument.
    :return: ModelSerializer class
    """

    # default values
    serializer = serializer or serializers.ModelSerializer

    attrs = {'model': model}
    if fields == '__all__':
        opts = model._meta.concrete_model._meta
        attrs['fields'] = [field.name for field in opts.fields if field.serialize]
    elif fields is not None:
        attrs['fields'] = fields
    if exclude is not None:
        attrs['exclude'] = exclude

    # create meta class
    parent = (object,)
    Meta = type('Meta', parent, attrs)

    # Give this new serializer class a reasonable name.
    class_name = model.__name__ + 'Serializer'

    # Class attributes for the new serializer class.
    serializer_class_attrs = {
        'Meta': Meta,
    }
    return type(serializer)(class_name, (serializer,), serializer_class_attrs)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号