代码之家  ›  专栏  ›  技术社区  ›  Keith John Hutchison

当不同模块中存在重复的类名时,获取django类引用的有效方法是什么?

  •  0
  • Keith John Hutchison  · 技术社区  · 6 年前

    我有一个函数,它采用类引用来创建SQL字符串。

    只要类名是唯一的,我就可以从globals()获取类引用。

    示例调用。

    module_name_a.models.get_insert_string(globals()['DriverShiftPreference'])
    

    如果在不同的模块中有重复的类名,那么会发生冲突。 例如,如果存在module_name_a.models.driver和module_name_b.models.driver,则

    module_name_a.models.get_insert_string(globals()['Driver'])
    

    从module_name_b.models.driver返回结果。

    当不同模块中存在重复的类名时,获取django类引用的有效方法是什么?

    2 回复  |  直到 6 年前
        1
  •  0
  •   Keith John Hutchison    6 年前

    使用inspect找到的方法最多。

    # in a module called junction
    import inspect 
    def get_insert_string(class_reference):
        fields = []
        arguments = []
        for field in class_reference._meta.fields:
            if field.primary_key:
                pass
            elif field.help_text:
                fields.append('"%s"' % field.help_text)
                arguments.append('?')
        result = 'insert into %s ( %s ) values ( %s )' % ( class_reference.filemaker_table_name, u','.join(fields), u','.join(arguments)) 
        return result 
    
    def make_models_map_from_module(module_):
        result = {}
        for item in inspect.getmembers(module_.models):
            key = item[0]
            value = item[1]
            if key == '__builtins__':
                break
            else:
                result[key] = value
        return result
    

    样品用途:

    import junction
    import depot_maestro
    depot_maestro_model_map = junction.models.make_models_map_from_module(depot_maestro)
    junction.models.get_insert_string(depot_maestro_model_map['Driver'])
    u'insert into driver ( "DEPOT_ID","uuid","ABN_number","account_name","address_1","advance_payment_days","bank_account","barred_flag","biometric_print","bsb","change_log","comm_fixed_percent","company","contract_end_date","contract_fixed","contract_notice_period","contract_start_date","contract_terms","country","date_activated","DOB","default_roster_template_id","EFT_code","email","external_id","fax","first_name","fuel_pin","fuel_split_percent","gender","is_owner","known_as","last_contacted","middle_name","nationality","Next_of_kin","next_of_kin_mobile","Next_of_kin_phone","next_of_kin_relationship","next_of_kin_work","passport","password","pay_in_choice","phone","pref_age","pref_body_type","pref_name_display","pref_transmission","remit_gst_for_ATO_who_remits","roster_template_id","status","surname","toll_serial_no","tsl_owner","unreliable_flag","www_LastKnown_GPS","www_LastKnown_Status","www_LastKnown_Timestamp","creationAccount","creationTimeStamp","modificationAccount","modificationTimeStamp" ) values ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )'
    
        2
  •  0
  •   Keith John Hutchison    6 年前

    找到了一个更简单的方法。把模型传过来。

    例子。

    junction.models.get_insert_string(depot_maestro.models.Group)
    

    谁会回来?

    插入到组中(“Depot_id”、“Uuid”、“Notes”、“Percentage”、“ABN”、“Address”、“Admin_Fee”、“Bank_Details”、“Bookings_Percent”、“CreationAccount”、“CreationTimestamp”、“Email”、“Fax”、“Group_Account_Name”、“Group_Color”、“Group_Logo”、“Group_Logo_2”、“Group_Name”、“Group_Number”、“Group_Service_Mark_as_Payed”、“GST“,”message“,”meter“,”modificationaccount“,”modificationtimestamp“,”omit_from_group_reports“,”parts_at_cost“,”phone“,”dermit_all_cash“,”rollover_amount“,”roster_group“,”sort_order“,”vehicle_fk”)值(???????????????????????????????????