2

My models.py:

class Channel(models.Model):
    title = models.CharField(max_length=255)

    def snapshot_statistics(self):
        new_channel_stat = ChannelStatistic(channel=self)
        new_channel_stat.save()


class ChannelStatistic(models.Model):
    channel = models.ForeignKey(Channel, on_delete=models.CASCADE)
    view_count = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        self.view_count = 3,
        super(ChannelStatistic, self).save(*args, **kwargs)

when triggering snapshot_stastistics() i get the following error:

int() argument must be a string, a bytes-like object or a number, not 'tuple'

in the django debug i can see this:

values  
[(<django.db.models.fields.related.ForeignKey: channel>, None, 35),
 (<django.db.models.fields.IntegerField: view_count>, None, (3,)),

django treats my assignment of 3 to the view_count attribute a as tupel.

Whats the matter of this behavior? How can i solve it?

Thanks in advance!

selli69
  • 41
  • 5

1 Answers1

9

There is an unneeded comma here:

self.view_count = 3,

That creates a tuple.

Nadège
  • 931
  • 6
  • 12