我尝试做的是将人们放在随机组中,但我不希望任何人在一个组中使用相同的名字和姓氏两次,因为那个人很可能在数据库中两次(他们&#39 ; d有不同的伙伴)
所以我有这个代码: (我将在djnago admin中将其用作动作)
型号:
class people(models.Model)
fname = models.CharField()
lname = models.CharField()
group = models.IntegerField()
partner = model.CharField()
查看:
N = 4
Num = randint(0, N-1)
for x in queryset:
x.group = Num
(if group == group & fname == fname & lname == lname:)<--Thats the part I am confused about, how can I check to see if these fields are the same, if they are, I want to change the group number field)
x.group = (Num + 1) % N
答案 0 :(得分:1)
考虑使用Unique Together。更改您的模型如下
class people(models.Model)
fname = models.CharField()
lname = models.CharField()
group = models.IntegerField()
partner = model.CharField()
class Meta:
unique_together = ('fname', 'lname', 'group',)