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

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

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

  1. class Action(models.Model):
  2. name = models.CharField(verbose_name="Name", max_length=100, blank=True)
  3. class ValidationModel(models.Model):
  4. user = models.ForeignKey(UserProfile, blank=True, null=True, on_delete=models.CASCADE)
  5. venue = models.ForeignKey(Action, blank=True, null true, on_delete=models.CASCADE)
  6. created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)
  7. class Task(models.Model):
  8. title = models.CharField(verbose_name="title", max_length=100, null=True, blank=True)
  9. venue = models.ManyToManyField(Action, blank=True)
  10. created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)
  11. class TaskAccepted(models.Model):
  12. name = models.ForeignKey(Task, null=True, blank=True, on_delete=models.SET_NULL, related_name='task_accepted')
  13. user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
  14. accepted_on = models.DateTimeField(auto_now_add=True, null=True, blank=True)

views

  1. def function(request, taskaccepted_id):
  2. instance_1 = Action.objects.filter(task__id=taskaccepted_id)
  3. action_count = instance_1.count()
  4. instance_2 = get_object_or_404(Task, pk=taskaccepted_id)
  5. sum_completed = 0 # Initialize sum_completed as an integer
  6. for action in instance1:
  7. for in_action in action.validationmodel_set.all()[:1]:
  8. latest_point = in_action.created_at
  9. action_completed = in_action
  10. if latest_point > instance_2.accepted_on:
  11. action_completed = 1
  12. else:
  13. action_completed = 0
  14. 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

  1. class Action(models.Model):
  2. name = models.CharField(verbose_name="Name",max_length=100, blank=True)
  3. class ValidationModel(models.Model):
  4. user = models.ForeignKey(UserProfile, blank=True, null=True, on_delete=models.CASCADE)
  5. venue = models.ForeignKey(Action, blank=True, null=True, on_delete=models.CASCADE)
  6. created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)
  7. class Task(models.Model):
  8. title = models.CharField(verbose_name="title",max_length=100, null=True, blank=True)
  9. venue = models.ManyToManyField(Action, blank=True)
  10. created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)
  11. class TaskAccepted(models.Model):
  12. name = models.ForeignKey(Task,null=True, blank=True, on_delete=models.SET_NULL, related_name='task_accepted')
  13. user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
  14. accepted_on = models.DateTimeField(auto_now_add=True, null=True, blank=True)

views

  1. def function(request, taskaccepted_id):
  2. instance_1 = Action.objects.filter(task__id=taskaccepted_id)
  3. action_count = instance_1.count()
  4. instance_2 = get_object_or_404(Task, pk=taskaccepted_id)
  5. sum_completed =()
  6. for action in instance1:
  7. for in_action in action.validationmodel_set.all()[:1]:
  8. latest_point = in_action.created_at
  9. action_completed = in_action
  10. if latest_point > instance_2.accepted_on:
  11. action_completed = 1
  12. else:
  13. action_completed = 0
  14. sum_completed += venue_completed #<-- can only concatenate tuple (not "int") to tuple

答案1

得分: 0

我的问题在这里:

  1. sum_completed =()

应该是

  1. sum_completed =0

而且@ivvija有效。

总之,如果有人遇到和我一样的情况:

  1. def function(request, taskaccepted_id):
  2. instance_1 = Action.objects.filter(task__id=taskaccepted_id)
  3. action_count = instance_1.count()
  4. instance_2 = get_object_or_404(Task, pk=taskaccepted_id)
  5. sum_completed = 0
  6. for action in instance1:
  7. for in_action in action.validationmodel_set.all()[:1]:
  8. latest_point = in_action.created_at
  9. action_completed = in_action
  10. if latest_point > instance_2.accepted_on:
  11. action_completed = 1
  12. else:
  13. action_completed = 0
  14. sum_completed += action_completed
英文:

My problem was here:

  1. sum_completed =()

It should have been

  1. sum_completed =0

and @ivvija works.

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

  1. def function(request, taskaccepted_id):
  2. instance_1 = Action.objects.filter(task__id=taskaccepted_id)
  3. action_count = instance_1.count()
  4. instance_2 = get_object_or_404(Task, pk=taskaccepted_id)
  5. sum_completed =0
  6. for action in instance1:
  7. for in_action in action.validationmodel_set.all()[:1]:
  8. latest_point = in_action.created_at
  9. action_completed = in_action
  10. if latest_point > instance_2.accepted_on:
  11. action_completed = 1
  12. else:
  13. action_completed = 0
  14. 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:

确定