Django如何迭代从ldap返回的列表以保存新对象

时间:2022-12-04 19:28:05

I have the following model:

我有以下型号:

class SystemUsers(models.Model):
    username = models.CharField(max_length=25)
    displayName = models.CharField(max_length=100)
    phoneNumber = models.BigIntegerField(max_length=10)
    emailAddress = models.EmailField(max_length=100)
    employeeNumber = models.CharField(max_length=7)
    firstName = models.CharField(max_length=20)
    lastName = models.CharField(max_length=30)

I have an ldap query that is returning the following from active directory:

我有一个ldap查询从活动目录返回以下内容:

user_details = []
            for entry in results:
                user_details.append(entry[1]['sAMAccountName'][0].lower())
                user_details.append(entry[1]['displayName'][0].replace(",", " "))
                user_details.append(entry[1]['telephoneNumber'][0].replace("-", ""))
                user_details.append(entry[1]['mail'][0].lower())
                user_details.append(entry[1]['employeeID'][0].lower())
                user_details.append(entry[1]['givenName'][0])
                user_details.append(entry[1]['sn'][0])

                return user_details

I am getting results as expected but I can't parse out the list to put it into a SystemUser(....).save() block to save it to the database. All I get is a list I can't loop over or set variables for.

我正在按预期获得结果,但我无法解析列表以将其放入SystemUser(....)。save()块以将其保存到数据库中。我得到的只是一个我无法循环或设置变量的列表。

When I do a

当我做的时候

for item in user_details:
     print(item)

All I have are 7 lines of values. I can't get it so that item[0] is username, item[1] is displayname, etc, etc

我只有7行数值。我无法得到它,因此item [0]是用户名,item [1]是displayname等

1 个解决方案

#1


1  

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

您可以使用用户数据创建dict并使用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?

BTW可能是从一开始就将user_details作为字典获得更好的想法?

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)

#1


1  

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

您可以使用用户数据创建dict并使用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?

BTW可能是从一开始就将user_details作为字典获得更好的想法?

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)