在django/python中,只能在if语句后将tuple(不是“int”)连接到tuple - sum up 2个变量

wwtsj6pe  于 2023-04-13  发布在  Go
关注(0)|答案(1)|浏览(80)

我希望将两个变量加在一起并得到错误:“只能将元组(不是“int”)连接到元组”(自原始帖子以来编辑的错误)
总而言之,我有一个todo列表。每次通过创建Validation对象模型来验证action时,action将获得1分。
如果task中的所有actions都获得了1分,则认为task完成。
我被分数的总和卡住了。
我觉得我需要在if语句后面写一个for循环来把每个动作中的每个点加在一起。我尝试了不同的组合,但似乎都不起作用。
(我确信我的代码也远不是最优的,所以如果你提供一个替代方案,我不会生气)

型号

class Action(models.Model):
    name = models.CharField(verbose_name="Name",max_length=100, blank=True)

class ValidationModel(models.Model):
    user = models.ForeignKey(UserProfile, blank=True, null=True, on_delete=models.CASCADE)
    venue = models.ForeignKey(Action, blank=True, null=True, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)

class Task(models.Model):
    title = models.CharField(verbose_name="title",max_length=100, null=True, blank=True)
    venue = models.ManyToManyField(Action, blank=True)
    created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)
    
class TaskAccepted(models.Model):
    name = models.ForeignKey(Task,null=True, blank=True, on_delete=models.SET_NULL, related_name='task_accepted')
    user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
    accepted_on = models.DateTimeField(auto_now_add=True, null=True, blank=True)

浏览次数

def function(request, taskaccepted_id):
    instance_1 = Action.objects.filter(task__id=taskaccepted_id)
    action_count = instance_1.count()
    instance_2 = get_object_or_404(Task, pk=taskaccepted_id)
sum_completed =()

    for action in instance1:
        
        for in_action in action.validationmodel_set.all()[:1]:
            latest_point = in_action.created_at
            action_completed = in_action
            if latest_point > instance_2.accepted_on:
                action_completed = 1   
            else:
                action_completed = 0
            sum_completed += venue_completed #<-- can only concatenate tuple (not "int") to tuple
vkc1a9a2

vkc1a9a21#

我的问题在这里:

sum_completed =()

本来应该是的

sum_completed =0

@ivvija工作。
最后,如果有人最终和我一样:

def function(request, taskaccepted_id):
    instance_1 = Action.objects.filter(task__id=taskaccepted_id)
    action_count = instance_1.count()
    instance_2 = get_object_or_404(Task, pk=taskaccepted_id)
sum_completed =0

    for action in instance1:
        
        for in_action in action.validationmodel_set.all()[:1]:
            latest_point = in_action.created_at
            action_completed = in_action
            if latest_point > instance_2.accepted_on:
                action_completed = 1   
            else:
                action_completed = 0
            sum_completed += action_completed

相关问题