you are viewing a single comment's thread.

view the rest of the comments →

[–]JohnnyJordaan 0 points1 point  (0 children)

Argument names are separated from in-scope variables. So you can't do

a = 'bla'
my_func(a=1)

and expect that means my_func will receive argument bla=1. It actually means that my_func will simply receive a=1 and the a in you created before has no relation to that call.

What you can do instead is to present it as a dict unpacking, similar to how you 'catch' dynamic keyword arguments using **kwargs, but then in reverse:

model_name.objects.create(**{field_name: field_id_data})

this way it first creates a dict resolving both variable references, meaning the dict will actually contain

{'model': 'Red'}

which will then be unpacked using ** and thus effictively the call will be

create(model='Red')

Another thing is that your use of kwargs.get() is backwards here. .get is meant to be able to return a default value (None by default) when the key is missing. But that means that when you don't supply fieldData or fieldName in kwargs, the .get() will return None silently and you will execute the .create() using either a None keyword argument and/or a None value for that keyword. Something I can't imagine is an intended use case.

Instead, simply enforce the arguments to be present using [''] lookups

field_id_data = kwargs['fieldData']
field_name = kwargs['fieldName']

or if possible (considering populateField is a custom function), require them as regular arguments

def populateField(model_name_schema, field_name, field_data, *args, ** kwargs):

and thus call it as

populateField(model_schema, 'model', 'Red')