【发布时间】:2014-08-08 03:41:48
【问题描述】:
我试图用下面的代码做的是一种组检查。我已使用组号将人员分配到组,因此:
fname lname group
mark anthony 2
macy grey 3 etc..
我想防止任何两个名字和姓氏相同的人在同一个组中。所以我写了一个动作来尝试这样做,但我想我可能发现自己陷入了无限循环,无论哪种方式都不起作用。
def groupcheck(modeladmin, request, queryset):
groups=[]
N = 7 # number of groups
for X in queryset:
item = "".join([X.fname, X.lname, str(X.group)]) #the information I am trying to check
if item in groups: # check if it is the array above (groups)
while item in groups:
G = X.group
Y = int(G) + int(1) #change the group number
if Y > N: # This is my attempt to not allow the group number to be more than the number of groups i allowed (N)
Y = 0
removeditemnumber = ''.join([i for i in item if not i.isdigit()]) # I am removing the group number
item = removeditemnumber + str(Y) # now I am trying to replace it
groups.append(item) #once the loop is done i want the edited version to be added to the array
X.group = Y
X.save() # I want the new group number to be saved to the databse
else:
groups.append(item)
我尝试添加 cmets 以帮助指导代码,以防我做错了,如果这让我分心,请告诉我,我会删除它们,如果可以,请提供帮助
【问题讨论】:
标签: python django django-admin django-views