can only concatenate tuple (not "int") to tuple – sum up 2 variables after an if statement in django/python

huangapple go评论69阅读模式
英文:

can only concatenate tuple (not "int") to tuple - sum up 2 variables after an if statement in django/python

问题

I am looking to sum 2 variables together and getting error: 'can only concatenate tuple (not "int") to tuple' (error edited since original post)

In summary, I have a sort of todo list. Every time an action is validated by creating a Validation object model, the action is given 1 point.

If all actions within the task have received 1 point, then the task is considered completed.

I am stuck at the sum of points.

I feel I need to write a sort of for loop after my if statement to add up each point in each action together. I tried different combos, but none seem to work.

Am I getting this wrong? (I am sure my code is also far from being optimal, so I won't be offended if you offer an alternative)

models

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)

views

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  # Initialize sum_completed as an integer
    
    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  # Corrected variable name

I have made some corrections to your code. The key change is initializing sum_completed as an integer (0) and also using the correct variable name (action_completed) to accumulate the points.

英文:

I am looking to sum 2 variables together and getting error: 'can only concatenate tuple (not "int") to tuple' (error edited since original post)

In summary, I have a sort of todo list. Every time an action is validated by creating an Validation object model, the action is given 1 point.

If all actions within the task have received 1 point, then the task is considered completed.

I am stuck at the sum of points.

I feel I need to write a sort of for loop after my if statement to add up each point in each action together. I tried different combos, but none seem to work.

Am I getting this wrong? (I am sure my code is also far from being optimal, so I wont be offended if you offer an alternative)

models

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)

views

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

答案1

得分: 0

我的问题在这里:

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
英文:

My problem was here:

sum_completed =()

It should have been

sum_completed =0

and @ivvija works.

To conclude, if someone end up in the same scenario as me:

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

huangapple
  • 本文由 发表于 2023年3月31日 19:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定