如何在django python中从另外两个表更新一个表对象

edqdpe6u  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(87)

我有两张table,一张给(用户),另一张给(礼物)。我想用这些表对象更新一个表(分布),以便在用户数和礼物数相同(3个用户和3个礼物)的情况下,让每个用户都得到一份礼物。但是如果用户和礼物不相等,在这种情况下,额外的礼物将被分配给任何用户,这意味着用户之一将被给予2个礼物。
如何在Django视图中实现这一点?先谢谢你了。
Models.py:

class GiftAssignment(models.Model):
        gift_id = models.IntegerField(
            default=0, blank=True, null=True
        )
        gift_name = models.CharField(
            max_length=35, blank=True, null=True
        )
        user_name = models.CharField(
            max_length=35, blank=True, null=True
        )
        user_id = models.IntegerField(
            default=0, verbose_name="User ID", blank=True, null=True
        )

views.py:

temp_arr = []

if remaining == 0:
        i = number #No. of gifts.
        z = number #No. of users.
        while z > 0:
            while i > 0:  # this is to add users to DB
                obj = Gifts.objects.exclude(pk__in=temp_arr)

                for g in obj:
                    g_pk = g.pk
                    g_n = g.gift_name
                    temp_arr.append(g_pk)

                    GiftAssignment.objects.create(
                        gift_id=g_pk,
                        gift_name=g_n,
                    )
                i -= 1

            z -= 1

elif remaining != 0:
     i = number
     z = number
     while z > 0:
        while i > 0: 
          obj1 = Gifts.objects.exclude(pk__in=temp_arr)
             for g in obj1:  # getting gifts are not in the array.
                 g_pk = g.pk
                 g_n = g.gift_name
                    
                    GateAssignment.objects.create(
                        gift_id=g_pk1,
                        gift_name=g_n1,
                        # user_id=u_pk1,
                        # user_name=u_n1,
                    )
                i -= 1
                temp_arr.append(g_pk)
            z -= 1

        # if there is a remaining.
        x = remaining
        # this to add the remaining
        d = GiftAssignment.objects.all()
        for pks in d:
            r_pk = pks.gift_id
        obj = Gifts.objects.exclude(pk__in=temp_arr)
        for g in obj:
            g_pk = g.pk
            g_n = g.gift_name
        for u_info in users: # users table.
            u_pk = u_info.user_id
            u_n = u_info.name
        while x > 0: 
            GiftAssignment.objects.create(
                gift_id=g_pk1,
                gift_name=g_n1,
                user_id=u_pk1,
                user_name=u_n1,
            )
            x -= 1
            temp_arr.append(r_pk)
zhte4eai

zhte4eai1#

我解决了这个问题如下:

users = users = User.objects.all()
gifts = Gifts.objects.all()
num_users = len(users)
num_gifts = len(gifts)

if num_users == num_gifts:
    for i in range(num_users):
        GiftAssignment.objects.create(user_name=users[i], gift_name=gift[i])
else:
    for i in range(num_gifts):
        GiftAssignment.objects.create(user_name=users[i % num_users], gift_name=gifts[i])
    GiftAssignment.objects.create(user_name=users[num_users - 1], gift_name=gifts[0])

相关问题