【问题标题】:How can I check that two records contain the same information in 3 specific fields?如何检查两条记录在 3 个特定字段中是否包含相同的信息?
【发布时间】:2014-09-20 14:36:22
【问题描述】:

我想要做的是将人们随机分组,但我不希望一个组中名字和姓氏相同的任何人两次,因为那个人很可能在数据库中两次(他们会有一个不同的合作伙伴)

所以我有这个代码: (我将在 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

【问题讨论】:

    标签: django django-admin django-views django-orm


    【解决方案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',)
    

    【讨论】:

    • 我如何将它应用到我上面的代码中 x.group = Num 后跟 (if !unique_togther) ?
    猜你喜欢
    • 2019-12-27
    • 2014-01-15
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 2011-04-25
    • 1970-01-01
    相关资源
    最近更新 更多