[Answer]-Django How to iterate over list returned from ldap to save new object

1👍

You can create the dict with the user data and use the kwargs magic:

field_names = ('username', 'displayName', 'phoneNumber', 'emailAddress',
               'employeeNumber', 'firstName', 'lastName', )
data = dict(zip(field_names, user_details)
SystemUser.objects.create(**data)

BTW may be it is a better idea to get the user_details as a dictionary from the beginning?

user_details = {
    'username': entry[1]['sAMAccountName'][0].lower(),
    'displayName': entry[1]['displayName'][0].replace(",", " "),
    'phoneNumber': entry[1]['telephoneNumber'][0].replace("-", ""),
    'emailAddress': entry[1]['mail'][0].lower(),
    'employeeNumber': entry[1]['employeeID'][0].lower(),
    'firstName': entry[1]['givenName'][0],
    'lastName': entry[1]['sn'][0],
}
SystemUser.objects.create(**user_details)

Leave a comment