Django 动态操作model

from django.apps import apps
            for o in oids_result:
                oid_result = snmp.snmpWalk(o.oid)
                if o.model_filed_name and len(o.model_filed_name) > 0:
                    field_name = o.model_filed_name.get('field_name', '')
                    '''
                    # 动态反射写法
                    app_name = o.model_filed_name.get('app_name', '')
                    model_name = o.model_filed_name.get('model_name', '')
                    model_obj = apps.get_model(app_name, model_name)
                    '''
                    if oid_result and len(oid_result):
                        snmp_result = oid_result[0][1]
                        item.__dict__.update({field_name: snmp_result})
                        item.save()
                print("snmp结果", oid_result)

 

posted @ 2023-07-31 20:07  汪丛兴  阅读(44)  评论(0编辑  收藏  举报