# lib/gen_admin.py import inspect import os from pathlib import Path import sys
import django
defget_classes(arg): """找出模块里所有的类""" classes = []
clsmembers = inspect.getmembers(arg, inspect.isclass) for (_, value) in clsmembers: classes.append(value)
return classes
defgen_admin(model): """根据模型生成 admin.py 的内容""" temp = f""" @admin.register({model.__name__}) class {model.__name__}Admin(admin.ModelAdmin): list_display = ('{"','".join((field.name for field in model._meta.fields))}') """ return temp
defgen_admins(models): result = "" for model in models: result += gen_admin(model) return result