Search code examples
django-modelsfilemaker

How do you reference a Django class name into a function?


I've written a function that returns the insert string for one class within Django that models and maps a table in FileMaker.

@staticmethod
def get_insert_string():
    fields = []
    arguments = []
    for field in Owner._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 )' % ( Owner.filemaker_table_name, u','.join(fields), u','.join(arguments)) 
    return result 

Example output.

Owner.get_insert_string()

u'insert into owner ( "uuid","ABN_number","Address_1","Creation_Date","Creation_Time","Creation_User","Creation_timestamp","Date_started","Modification_timestamp","State","Suburb","account_code","account_name","account_number","authorised_officer_first_name","authorised_officer_signature","authorised_officer_surname","bank_name","bsb","company_name","company_use_as_address","crn_number","discount_percent","driver_or_staff","fax","is_driver","is_staff","jp_name","jp_number","jp_signature","landline","mobile","oa_number","owner_1_first_name","owner_1_middle_name","owner_1_surname","owner_2_first_name","owner_2_middle_name","owner_2_surname","owner_3_first_name","owner_3_middle_name","owner_3_surname","paid_always","parts_at_cost","post code","purchase_order_required","shareholder","status","taxable","taxable_export","thirty_day_account","trade_retail_for_parts_cost","tsl_number","tsl_owner","updated_date" ) values ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )'

How could this be changed so the name of the class could be passed in and get the same results so that the function could be used for all the classes in the module?

Something like

def get_insert_string(class_name):
    fields = []
    arguments = []
    for field in class_name._meta.fields:
        if field.primary_key:
            pass
        if field.help_text:
            fields.append('"%s"' % field.help_text)
            arguments.append('?')
    result = 'insert into %s ( %s ) values ( %s )' % ( class_name.filemaker_table_name, u','.join(fields), u','.join(arguments)) 
    return result 

where class_name maps into the matching Django class.


Solution

  • Discovered globals() mostly covers it.

    Function example.

    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 
    

    Calling example:

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

    Results example:

    insert into driver_shift_preferences ( "DEPOT_ID","driver_id","roster_template_id","roster_template____shift_slot_id","vehicle_id","uuid","commission_percentage","drive_name_lookup","lease_fee","levy_1","levy_2","linked_to_id","note","selected","shift_slot","week_id","creation_account_name","created_timestamp","modification_account_name","modification_timestamp" ) values ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )'
    

    Only issue remaining is how to reference a class where the same class name exists in different modules.