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!

2
  • Yes, you did assign a tuple: self.view_count = 3,. See that comma? Commented Aug 1, 2017 at 8:44
  • Thanks a lot. I should take more breaks when working overnight.. ;) Commented Aug 1, 2017 at 8:48

1 Answer 1

9

There is an unneeded comma here:

self.view_count = 3,

That creates a tuple.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. I should take more breaks when working overnight.. ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.