[Answer]-Dynamic keyword with database value

0👍

You can use setattr to dynamically set attributes to the objects.

1👍

You can also dynamically build a dict from your dict (you shouldn’t use a builtin name as identifier BTW) and pass it as kwargs to meter1() (you shouldn’t use all-lowers as class names BTW). Oh and yes: you may want to have a look at modelmanager.create() too.

def import_data():
    fieldmap = {"1":"U1N","2":"U2N",}
    fieldindex = "1"
    kw = {fieldmap[fieldindex]: 1.0}

    # either:
    q = Meter1(**kw)
    q.save()

    # or:
    q = Meter1.objects.create(**kw)

Leave a comment